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?
You must declare
xoutsideswitch. and declare it only once. and if classes does not have same parent you must usedynamicas a type of x.