Declare different type of variables using switch

1.5k views Asked by At

I am trying to create a new variable based on a particular value received from a function.

The variable can be of different classes.

Here's what I am trying:

switch (request)
{
    case "ClassA":
        {
            var x = new ClassA();
            break;
        }
    case "ClassB":
        {
            var x = new ClassB();
            break;
        } 
    case "ClassC":
        {
            var x = new ClassC();
            break;
        } 
    case "ClassD":
        {
            var x = new ClassD();
            break;
        }
    default:
        break;
}

This is fine till here and no issues. The issue arises, when I try to use the value of x out of the scope of the switch statement. The system says that x does not exists in the current context.

Is there any way to achieve this?

3

There are 3 answers

0
M.kazem Akhgary On BEST ANSWER

You must declare x outside switch. and declare it only once. and if classes does not have same parent you must use dynamic as a type of x.

ParentClass x = null;// dynamic x = null; in case that x is not known type
switch (request)
        {
            case "ClassA":
                {
                    x = new ClassA();
                    break;
                }
            case "ClassB":
                {
                    x = new ClassB();
                    break;
                } 
            case "ClassC":
                {
                    x = new ClassC();
                    break;
                } 
            case "ClassD":
                {
                    x = new ClassD();
                    break;
                }
            default:
                break;
        }
1
David Arno On

Rather than use a switch statement, use a Dictionary to provide the class lookup:

private readonly Dictionary<string, Func<object>> classLookup = 
    new Dictionary<string, Func<object>>
{
    { "ClassA", () => new ClassA() },
    { "ClassB", () => new ClassB() },
    { "ClassC", () => new ClassC() },
    { "ClassD", () => new ClassD() }
};

public object GetObject(string className)
{
    return classLookup.ContainsKey(className)
        ? classLookup[className]()
        : null;
}
0
Der Kommissar On

This looks like a good place for an interface or abstract class.

Basically, to most easily solve your issue, you would implement an interface as follows:

interface IDescriptiveName
{
    DescriptiveNameType Type { get; }
}

enum DescriptiveNameType
{
    ClassA
}

Each class would then implement DescriptiveNameType Type and return their type. (Generally DescriptiveNameType is an enum.) I.e.

public class ClassA : IDescriptiveName
{
    public DescriptiveNameType Type { get { return DescriptiveNameType.ClassA; } }
}

Then, based on the value of ClassA.Type (which for ClassA would be ClassA) you could cast x and work with it.

IDescriptiveName x = null;

// original switch logic simply remove var

if (x.Type == DescriptiveNameType.ClassA)
{
    // do something for ClassA
}