PC Review


Reply
Thread Tools Rate Thread

Creating an object with a variable

 
 
DaveD
Guest
Posts: n/a
 
      24th Jul 2006
OK, this may be simple, but...

I have a 5 classes, they all bring up a form to edit a table, and
they're all derivied from a virtual class. The user is allowed to pick
a table to edit from a drop-down list that was populated from a SQL
table, this table also contains the name of the class that is used to
edit it. Can I use 1 new statment to create the correct object or do I
have to use a switch?

Thanks,
Dave
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      24th Jul 2006
Dave,

You could use a switch, but in reality, you will want to look at the
static GetType method on the Type class. With this, assuming you have a
fully qualified type name, you can get the type that you have stored in the
database. Then, you can pass that Type instance to the static
CreateInstance method on the Activator class, and it will return an instance
of that type.

This assumes that they all have the same constructor of course
(parameterless, or same number/type of arguments).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"DaveD" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> OK, this may be simple, but...
>
> I have a 5 classes, they all bring up a form to edit a table, and
> they're all derivied from a virtual class. The user is allowed to pick
> a table to edit from a drop-down list that was populated from a SQL
> table, this table also contains the name of the class that is used to
> edit it. Can I use 1 new statment to create the correct object or do I
> have to use a switch?
>
> Thanks,
> Dave



 
Reply With Quote
 
Greg Young
Guest
Posts: n/a
 
      24th Jul 2006
I would recommend using the factory pattern to encapsulat the logic.

You can either use a switch or you can use Activator.CreateInstance() (or
more correctly you could include the assembly and use
Assembly.CreateInstance as the assembly that the type is in may have not
been loaded yet

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"DaveD" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> OK, this may be simple, but...
>
> I have a 5 classes, they all bring up a form to edit a table, and
> they're all derivied from a virtual class. The user is allowed to pick
> a table to edit from a drop-down list that was populated from a SQL
> table, this table also contains the name of the class that is used to
> edit it. Can I use 1 new statment to create the correct object or do I
> have to use a switch?
>
> Thanks,
> Dave



 
Reply With Quote
 
DaveD
Guest
Posts: n/a
 
      25th Jul 2006
Thanks, I knew that had to be a way of doing it.

Dave


On Mon, 24 Jul 2006 14:27:58 -0400, DaveD <(E-Mail Removed)>
wrote:

>OK, this may be simple, but...
>
>I have a 5 classes, they all bring up a form to edit a table, and
>they're all derivied from a virtual class. The user is allowed to pick
>a table to edit from a drop-down list that was populated from a SQL
>table, this table also contains the name of the class that is used to
>edit it. Can I use 1 new statment to create the correct object or do I
>have to use a switch?
>
>Thanks,
>Dave

 
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
How to get what a variable is dimmed as? How to get what an object isdeclared as? If variable or object is Nothing? recurr Microsoft VB .NET 4 10th Dec 2007 04:12 PM
SqlConnection Object in Application Variable OR Session Variable OR open/close pratham Microsoft ASP .NET 3 31st Aug 2006 08:26 PM
Creating an object using a type variable Ramez Microsoft VB .NET 2 27th Jan 2005 03:04 PM
Re: Creating object of variable type Jon Skeet Microsoft Dot NET 1 26th Sep 2003 09:04 PM
Re: Exel2000 VBA - Creating a Command Bar - Error: "Object Variable or With not Set" keepitcool Microsoft Excel Programming 0 11th Aug 2003 12:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:29 AM.