PC Review


Reply
Thread Tools Rate Thread

converting string to object

 
 
excelleinc.com
Guest
Posts: n/a
 
      15th Mar 2005
Hi,

I want to have a sub that takes string as argument and then opens a form
that has that string as a class name.

Say:

Class users
Inherits System.Windows.Forms.Form
'display form content
End Class

Sub functions(str As String)
Dim frm As New str 'this is want I'm trying to accomplish - obviously
doesn't work this way
frm.ShowDialog()
End Sub

And then call it as Sub("users") If I want to accomplish next:
Dim frm As New users
frm.ShowDialog()


Obviously, what I'm trying to do is much more complicated but if I would
know how to accomplish example above I'd be able to do what I need.


Thanks,

Vlado


http://www.excelleinc.com


 
Reply With Quote
 
 
 
 
Daniel Klein
Guest
Posts: n/a
 
      15th Mar 2005
On Tue, 15 Mar 2005 13:11:34 -0600, "excelleinc.com"
<(E-Mail Removed)> wrote:

>Obviously, what I'm trying to do is much more complicated but if I would
>know how to accomplish example above I'd be able to do what I need.


Look up Activator.CreateInstance() method in the MSDN.

Daniel Klein

 
Reply With Quote
 
Imran Koradia
Guest
Posts: n/a
 
      15th Mar 2005
You'll need to use reflection:

Imports System.Reflection

Sub ShowForm(str As String)
Dim ty As Type = Type.GetType(str)
Dim frm As Object = Activator.CreateInstance(ty)
DirectCast(frm, Form).ShowDialog()
End Sub


Then call the above method as:
ShowForm("MyApplication.users")

Note that the method Type.GetType requires a fully qualified name including
namespace. Since by default Vb.NET adds the project as the default namespace,
you would need to append it to the form name before passing it as a parameter
to Type.GetType (assuming you are using the default settings only). I would
suggest reading up on the documentation of Type.GetType to make sure you
don't end up with null references.


hope that helps..
Imran.


> Hi,
>
> I want to have a sub that takes string as argument and then opens a
> form that has that string as a class name.
>
> Say:
>
> Class users
> Inherits System.Windows.Forms.Form
> 'display form content
> End Class
> Sub functions(str As String)
> Dim frm As New str 'this is want I'm trying to accomplish -
> obviously
> doesn't work this way
> frm.ShowDialog()
> End Sub
> And then call it as Sub("users") If I want to accomplish next:
> Dim frm As New users
> frm.ShowDialog()
> Obviously, what I'm trying to do is much more complicated but if I
> would know how to accomplish example above I'd be able to do what I
> need.
>
> Thanks,
>
> Vlado
>
> http://www.excelleinc.com
>




 
Reply With Quote
 
excelleinc.com
Guest
Posts: n/a
 
      15th Mar 2005
Worked great.

I was getting null references but when I turned IgnoreCase to True
Dim ty As Type = Type.GetType(Str, False, True)
everything worked just great.

Thank you both very much.


"Imran Koradia" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> You'll need to use reflection:
>
> Imports System.Reflection
>
> Sub ShowForm(str As String)
> Dim ty As Type = Type.GetType(str)
> Dim frm As Object = Activator.CreateInstance(ty)
> DirectCast(frm, Form).ShowDialog()
> End Sub
>
>
> Then call the above method as:
> ShowForm("MyApplication.users")
>
> Note that the method Type.GetType requires a fully qualified name
> including namespace. Since by default Vb.NET adds the project as the
> default namespace, you would need to append it to the form name before
> passing it as a parameter to Type.GetType (assuming you are using the
> default settings only). I would suggest reading up on the documentation of
> Type.GetType to make sure you don't end up with null references.
>
>
> hope that helps..
> Imran.
>
>
>> Hi,
>>
>> I want to have a sub that takes string as argument and then opens a
>> form that has that string as a class name.
>>
>> Say:
>>
>> Class users
>> Inherits System.Windows.Forms.Form
>> 'display form content
>> End Class
>> Sub functions(str As String)
>> Dim frm As New str 'this is want I'm trying to accomplish -
>> obviously
>> doesn't work this way
>> frm.ShowDialog()
>> End Sub
>> And then call it as Sub("users") If I want to accomplish next:
>> Dim frm As New users
>> frm.ShowDialog()
>> Obviously, what I'm trying to do is much more complicated but if I
>> would know how to accomplish example above I'd be able to do what I
>> need.
>>
>> Thanks,
>>
>> Vlado
>>
>> http://www.excelleinc.com
>>

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting String object to XDocument object K Viltersten Microsoft C# .NET 4 7th Apr 2009 03:48 PM
Converting a string to a Form object Kevin S Gallagher Microsoft VB .NET 1 29th Sep 2006 12:40 PM
Converting Object into String... Wallace Microsoft C# .NET 3 29th Mar 2006 03:47 PM
Converting object to string. bryja_klaudiusz[at]poczta[dot]fm Microsoft C# .NET 1 1st Mar 2005 11:37 AM
Converting String into Object Rajesh Kr Shukla Microsoft VB .NET 5 22nd Sep 2003 12:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 AM.