Click here to Skip to main content
15,905,877 members
Home / Discussions / C#
   

C#

 
GeneralLogging Shutdown event in Windows Service Pin
abupsman31-Jan-08 7:46
abupsman31-Jan-08 7:46 
GeneralRe: Logging Shutdown event in Windows Service Pin
CKnig31-Jan-08 18:56
CKnig31-Jan-08 18:56 
QuestionHow to instantiate an object with a generic parameter? Pin
michal.kreslik31-Jan-08 7:31
michal.kreslik31-Jan-08 7:31 
AnswerRe: How to instantiate an object with a generic parameter? Pin
Giorgi Dalakishvili31-Jan-08 8:18
mentorGiorgi Dalakishvili31-Jan-08 8:18 
AnswerRe: How to instantiate an object with a generic parameter? Pin
Ennis Ray Lynch, Jr.31-Jan-08 9:00
Ennis Ray Lynch, Jr.31-Jan-08 9:00 
AnswerRe: How to instantiate an object with a generic parameter? Pin
Le centriste31-Jan-08 10:05
Le centriste31-Jan-08 10:05 
GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik31-Jan-08 10:26
michal.kreslik31-Jan-08 10:26 
GeneralRe: How to instantiate an object with a generic parameter? Pin
Le centriste31-Jan-08 12:59
Le centriste31-Jan-08 12:59 
I reexamined your example, and her is what I suggest. Since you only have 2 well-known concrete classes, you may not need reflection for now, but generics is not the way to go either.

Make an interface:

public interface IFoo
{
    void MyMethod(); // Interface defines one method.
}


Then have some implementation, 2 in you example:

public class FooA : IFoo
{
    public void MyMethod() { Console.WriteLine("Hello from FooA"); }
}

public class FooB : IFoo
{
    public void MyMethod() { Console.WriteLine("Hello from FooB"); }
}


Then, using your example code:

class Program
{
    static void Main()
    {
        Console.WriteLine("Press 1 for FooA, press 2 for FooB:");
        ConsoleKeyInfo key = Console.ReadKey(true);            
        IFoo myFoo = null;            

        if (key.KeyChar == '1')            
        {                
            myFoo = new FooA();            
        }            
        else if (key.KeyChar == '2')            
        {                
            myFoo = new FooB();            
        }
        else
        {
            throw new InvalidOperationException("Wrong selection, you must select 1 or 2"); // Always have a watch dog.
        }

        myFoo.MyMethod(); // This prints which foo was created.
    }
}


This example is pretty simplistic, but you get the idea. In this particular case, you didn't need generics nor reflection. In real-life application, this is rarely sufficient. Read on reflection, I am sure there are quite good articles on this site.

Good luck.

-----

You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies

If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

God is the only being who, to rule, does not need to exist. -- Charles Baudelaire

GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik31-Jan-08 13:17
michal.kreslik31-Jan-08 13:17 
AnswerRe: How to instantiate an object with a generic parameter? Pin
darkelv31-Jan-08 14:18
darkelv31-Jan-08 14:18 
GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik6-Feb-08 4:05
michal.kreslik6-Feb-08 4:05 
AnswerRe: How to instantiate an object with a generic parameter? Pin
Mark Churchill1-Feb-08 2:47
Mark Churchill1-Feb-08 2:47 
GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik6-Feb-08 22:12
michal.kreslik6-Feb-08 22:12 
GeneralRe: How to instantiate an object with a generic parameter? [modified] Pin
Mark Churchill7-Feb-08 0:07
Mark Churchill7-Feb-08 0:07 
GeneralRefresh Pin
kibromg31-Jan-08 7:14
kibromg31-Jan-08 7:14 
GeneralRe: Refresh Pin
pmarfleet31-Jan-08 8:43
pmarfleet31-Jan-08 8:43 
Generalmarshaling an array of structs Pin
damianrda31-Jan-08 6:23
damianrda31-Jan-08 6:23 
GeneralDeleting Outlook Appointments Pin
NewToAspDotNet31-Jan-08 6:15
NewToAspDotNet31-Jan-08 6:15 
GeneralNesting panels Pin
DSdragondude31-Jan-08 5:29
DSdragondude31-Jan-08 5:29 
QuestionRe: Nesting panels Pin
TJoe31-Jan-08 5:38
TJoe31-Jan-08 5:38 
GeneralMulti Colored text in a tree view. Pin
RKavanagh31-Jan-08 5:02
RKavanagh31-Jan-08 5:02 
GeneralRe: Multi Colored text in a tree view. Pin
Luc Pattyn31-Jan-08 5:26
sitebuilderLuc Pattyn31-Jan-08 5:26 
QuestionHow to make form as a model Window??? Pin
Neo Andreson31-Jan-08 4:48
Neo Andreson31-Jan-08 4:48 
AnswerRe: How to make form as a model Window??? Pin
Skippums31-Jan-08 4:53
Skippums31-Jan-08 4:53 
GeneralRe: How to make form as a model Window??? Pin
Neo Andreson31-Jan-08 17:46
Neo Andreson31-Jan-08 17:46 

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.