Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
Questionxaml page as resource Pin
Giorgi Nistor23-Jan-12 4:50
Giorgi Nistor23-Jan-12 4:50 
AnswerRe: xaml page as resource Pin
dexterama23-Jan-12 5:55
professionaldexterama23-Jan-12 5:55 
GeneralRe: xaml page as resource Pin
Giorgi Nistor23-Jan-12 6:24
Giorgi Nistor23-Jan-12 6:24 
Questionrun the batch file code in C# Pin
Member 853472622-Jan-12 23:47
Member 853472622-Jan-12 23:47 
AnswerRe: run the batch file code in C# Pin
Richard MacCutchan22-Jan-12 23:50
mveRichard MacCutchan22-Jan-12 23:50 
AnswerRe: run the batch file code in C# PinPopular
phil.o23-Jan-12 3:19
professionalphil.o23-Jan-12 3:19 
AnswerRe: run the batch file code in C# Pin
lukeer23-Jan-12 4:24
lukeer23-Jan-12 4:24 
QuestionHow to resolve class type using enum Pin
emmmatty122-Jan-12 20:05
emmmatty122-Jan-12 20:05 
I have a group of classes (following strategy pattern) in my project. In the main function, I receive an enum value from the server and based on that I create an object of the base class type.

I am using switch/case statement to achieve this. I read somewhere that the Open/Closed principle does not allow opening a function to add a new case statement whenever a new class is added.

I am thinking of using a Activator.CreateInstance(). Is there any drawback to it.

Is there any other way to create an object from the enum type?

Adding example below even though it is not a full fledged Strategy pattern

abstract public class Mammal
{
public abstract void MakeSound()
}

C#
class Cat:Mammal
{      
    public override  void MakeSound()
    {
        Console.WriteLine("Meow");        
    }    
}

class Dog:Mammal
{

    public override void MakeSound()
    {
         Console.WriteLine("Bow");        
    }    
}

Main()
{

    MammalTypes mammalType = RecieveValueFromServer();
    Mammal mammalBase
    switch(mammalType) // need to make this dynamic depending upon Enum type
    {
        case MammalTypes.Cat:mammalBase = new Cat()
                             break;
        case MammalTypes.Dog:mammalBase = new Dog()
                             break;            
    }

    mammalBase.MakeSound()
}

AnswerRe: How to resolve class type using enum Pin
OriginalGriff22-Jan-12 22:05
mveOriginalGriff22-Jan-12 22:05 
GeneralRe: How to resolve class type using enum Pin
emmmatty122-Jan-12 22:37
emmmatty122-Jan-12 22:37 
GeneralRe: How to resolve class type using enum Pin
OriginalGriff22-Jan-12 22:38
mveOriginalGriff22-Jan-12 22:38 
GeneralRe: How to resolve class type using enum Pin
PIEBALDconsult23-Jan-12 5:13
mvePIEBALDconsult23-Jan-12 5:13 
GeneralRe: How to resolve class type using enum Pin
jschell23-Jan-12 8:17
jschell23-Jan-12 8:17 
GeneralRe: How to resolve class type using enum Pin
BobJanova24-Jan-12 10:07
BobJanova24-Jan-12 10:07 
GeneralRe: How to resolve class type using enum Pin
jschell25-Jan-12 14:50
jschell25-Jan-12 14:50 
GeneralRe: How to resolve class type using enum Pin
Dean Oliver23-Jan-12 18:20
Dean Oliver23-Jan-12 18:20 
GeneralRe: How to resolve class type using enum Pin
BobJanova24-Jan-12 10:32
BobJanova24-Jan-12 10:32 
QuestionTask cancellation Pin
michaelgr122-Jan-12 3:59
michaelgr122-Jan-12 3:59 
AnswerRe: Task cancellation Pin
Dave Kreskowiak22-Jan-12 8:15
mveDave Kreskowiak22-Jan-12 8:15 
Questionhow to retrieve a single value from datatable Pin
mschotamaster21-Jan-12 22:01
mschotamaster21-Jan-12 22:01 
AnswerRe: how to retrieve a single value from datatable Pin
Richard MacCutchan21-Jan-12 23:01
mveRichard MacCutchan21-Jan-12 23:01 
GeneralRe: how to retrieve a single value from datatable Pin
mschotamaster22-Jan-12 3:12
mschotamaster22-Jan-12 3:12 
GeneralRe: how to retrieve a single value from datatable Pin
Richard MacCutchan22-Jan-12 3:15
mveRichard MacCutchan22-Jan-12 3:15 
GeneralRe: how to retrieve a single value from datatable Pin
mschotamaster22-Jan-12 3:17
mschotamaster22-Jan-12 3:17 
GeneralRe: how to retrieve a single value from datatable Pin
Richard MacCutchan22-Jan-12 3:23
mveRichard MacCutchan22-Jan-12 3:23 

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.