Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
AnswerRe: Setup Project on vs 2005 with framework Pin
fetras18-May-06 12:32
fetras18-May-06 12:32 
AnswerRe: Setup Project on vs 2005 with framework Pin
MoustafaS18-May-06 13:56
MoustafaS18-May-06 13:56 
AnswerRe: Setup Project on vs 2005 with framework Pin
fetras19-May-06 7:17
fetras19-May-06 7:17 
Questionobject.GetType() Question ??? Pin
CombatRob18-May-06 10:57
CombatRob18-May-06 10:57 
AnswerRe: object.GetType() Question ??? Pin
NaNg1524118-May-06 11:07
NaNg1524118-May-06 11:07 
AnswerRe: object.GetType() Question ??? Pin
CombatRob18-May-06 11:14
CombatRob18-May-06 11:14 
GeneralRe: object.GetType() Question ??? Pin
Rei Miyasaka18-May-06 11:54
Rei Miyasaka18-May-06 11:54 
GeneralRe: object.GetType() Question ??? Pin
Nick Parker18-May-06 12:00
protectorNick Parker18-May-06 12:00 
If you want to create a new instance of a particular type, take a look at Activator.CreateInstance. Notice that it will give you an Object or ObjectHandle back, depending on which overload you call. If you are programming to a interface or there is a base class to the type you are creatting at runtime, you can cast the returning object instance to that type (interface or base class), otherwise you will need your function to return type Object.

This changes with generics in .NET 2.0 however, you can declare a generic method similar to what you had define above, though the syntax looks more like this:

public class Builder
{
    public static T Create<T>() where T : class
    {
        T var = Activator.CreateInstance<T>();
        return var;
    }
}


Then I can just specify the type as such (assuming a Car and Truck class exist somewhere):
Car c = Builder.Create<Car>();
Person p = Builder.Create<Truck>();




- Nick Parker
Microsoft MVP - Visual C#
My Blog | My Articles

QuestionCasting an image from an ImageList to an Icon? Pin
LongRange.Shooter18-May-06 9:39
LongRange.Shooter18-May-06 9:39 
AnswerRe: Casting an image from an ImageList to an Icon? Pin
Rei Miyasaka18-May-06 9:54
Rei Miyasaka18-May-06 9:54 
GeneralRe: Casting an image from an ImageList to an Icon? Pin
led mike18-May-06 10:05
led mike18-May-06 10:05 
GeneralRe: Casting an image from an ImageList to an Icon? Pin
Rei Miyasaka18-May-06 10:10
Rei Miyasaka18-May-06 10:10 
GeneralRe: Casting an image from an ImageList to an Icon? Pin
led mike18-May-06 10:17
led mike18-May-06 10:17 
GeneralRe: Casting an image from an ImageList to an Icon? Pin
LongRange.Shooter19-May-06 4:18
LongRange.Shooter19-May-06 4:18 
QuestionHow i exit application Pin
cshivaprasad18-May-06 9:31
cshivaprasad18-May-06 9:31 
AnswerRe: How i exit application Pin
Rei Miyasaka18-May-06 9:54
Rei Miyasaka18-May-06 9:54 
AnswerRe: How i exit application Pin
friendshiple18-May-06 14:55
friendshiple18-May-06 14:55 
QuestionHow do I make my objects Database aware? Pin
martin_hughes18-May-06 9:22
martin_hughes18-May-06 9:22 
AnswerRe: How do I make my objects Database aware? Pin
Rei Miyasaka18-May-06 10:08
Rei Miyasaka18-May-06 10:08 
GeneralRe: How do I make my objects Database aware? Pin
martin_hughes18-May-06 22:21
martin_hughes18-May-06 22:21 
GeneralRe: How do I make my objects Database aware? Pin
Rei Miyasaka18-May-06 23:06
Rei Miyasaka18-May-06 23:06 
GeneralRe: How do I make my objects Database aware? Pin
martin_hughes19-May-06 1:32
martin_hughes19-May-06 1:32 
GeneralRe: How do I make my objects Database aware? Pin
Rei Miyasaka19-May-06 1:59
Rei Miyasaka19-May-06 1:59 
QuestionShow + Focus Problem Pin
Phoen2518-May-06 9:22
Phoen2518-May-06 9:22 
AnswerRe: Show + Focus Problem Pin
stancrm18-May-06 21:01
stancrm18-May-06 21:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.