Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / April 2007

Tip: Looking for answers? Try searching our database.

Name of type to class instance?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Smokey Grindle - 18 Apr 2007 15:00 GMT
Is it possible to create an object of a specific type just by knowing its
name as a text string? Say I have the string

System.Windows.Forms.Textbox how would I go about turing that string into an
object of that type? I think its possible with reflection, but not sure
how... thanks!
Phill W. - 18 Apr 2007 15:42 GMT
> Is it possible to create an object of a specific type just by knowing its
> name as a text string?

Almost certainly.

> I think its possible with reflection, but not sure how...

As soon as you start reaching for the Reflection tool box, take a step
back and ask yourself if there's not a better way to do what you need.

What is it that you want to achieve? (never mind how, for now)

Regards,
   Phill  W.
Smokey Grindle - 18 Apr 2007 15:59 GMT
Well we know for certain we have to create objects on the fly based off of a
type name specified in a database row... so right now we have the object
type... just need to create the object

>> Is it possible to create an object of a specific type just by knowing its
>> name as a text string?
[quoted text clipped - 10 lines]
> Regards,
>    Phill  W.
rowe_newsgroups - 18 Apr 2007 17:15 GMT
> Well we know for certain we have to create objects on the fly based off of a
> type name specified in a database row... so right now we have the object
[quoted text clipped - 14 lines]
> > Regards,
> >    Phill  W.

In addition to the others, if you are using serializable controls you
can store the searilized data into the database and then deserialize
the data back into the control. This way you don't have to worry about
setting all the properties.

Thanks,

Seth Rowe
Phill W. - 18 Apr 2007 17:18 GMT
> Well we know for certain we have to create objects on the fly based off of a
> type name specified in a database row... so right now we have the object
> type... just need to create the object

Ah ha!
That's one of the Good Reasons for doing this.

As Tom said, Type.GetType and Activator.CreateInstance.

This will work well for Framework-defined Types but watch out when you
start trying to use Types defined in /your own/ assemblies - whatever
process is doing all this has to be able to "get hold of" (i.e. Load)
the defining Assemblies.  Can make for some fun deployment issues.

HTH,
   Phill  W.
Tom Shelton - 18 Apr 2007 16:26 GMT
> Is it possible to create an object of a specific type just by knowing its
> name as a text string? Say I have the string
>
> System.Windows.Forms.Textbox how would I go about turing that string into an
> object of that type? I think its possible with reflection, but not sure
> how... thanks!

Topics to look at in the documentation:

Type.GetType
Activator.CreateInstance

That should be enought to get you started.

--
Tom Shelton
Smokey Grindle - 18 Apr 2007 16:34 GMT
awesome, thanks!

>> Is it possible to create an object of a specific type just by knowing its
>> name as a text string? Say I have the string
[quoted text clipped - 13 lines]
> --
> Tom Shelton

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.