Question

I need to declare a variable inside an if statement. Then I will use it on the outside. But as far as I know, there are no external variables in C#. But I need to do this.

I have two classes both derived from one class.

Base class: Operand Derived classes: NormalOperand SpecialOperand

bool normal

Declared somewhere

if(normal)
    NormalOperand o = stack.Pop() as NormalOperand;
else
    SpecialOperand o = stack.Pop() as SpecialOperand;

I don't want to deal this differences below. Is there any hack to do that? Or do I have to deal with it everywhere I do something related to this?

Was it helpful?

Solution

I don't see the issue, just declare o as the base class Operand.

Operand o = stack.Pop(); // add as Operand if needed

Later if you need to know the type of o (using virtual/abstract methods on base class should avoid this) then use:

if (o is NormalOperand)
{
    // TODO: maybe check for null
    (o as NormalOperand).NormalSpecificMethod();
}

OTHER TIPS

As I understand the only reason you want to declare such a varaible as external is because you want to call different methods on them.

For example:

 normal = true;

 ...

 o.DoSomethingNormal()

 normal = false;
 o.DoSomethingSpecial();

If you have a look at this code, and realize that C# is a statically typed language you will understand that this can never compile.

But if you want to call a method that is declared on both Normal and Special you could should declare this method in an Interface or base class and cast the stack.Pop() to that type. That way you will use polyformism and it will work.

In your case:

Operand o = stack.Pop() as Operand;
o.DoSomething();

Usually, you should be declaring the variable before the if statement, declaring it as an Operand. In this situation, you don't need an if statement at all, nor do you need the boolean normal. Just declare it as an Operand and use polymorphism to do whatever else it is you need to do.

Operand o = stack.Pop() as Operand;

As you say, you don't want to deal with the differences below, you shouldn't need to know whether o is special or not. Or do you?

For starters, your example doesn't make any sense as both those temporaries disappear anyway. But let's ignore that little bit.

Given that these operands are on the same stack, we can assume they are both derived from a common class. I'll assume that is Operand.

Operand o = Stack.Pop();

Now, I also assume you want to do something with this code. What is it? Why do you have to know? If you really just want to know what type it is, you can say:

if(o.GetType() == typeof(NormalOperand))
{
}
else if(o.GetType() == typeof(SpecialOpernad))
{
}

You should be able to do:

bool normal = ...

Operand o = null;
if (normal) o = stack.Pop() as NormalOperand; Enter code here`else o = stack.Pop() as SpecialOperand;

I am not sure I understand it though. I mean, this makes sense only if you need to do other stuff within the if-statement. otherwise, as George suggests, you can always do o = stack.Pop() and then check type using as and null checking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top