This seems to be a very stupid casting question, but ...
I have the following setup:
There are a number of classes derived from the main class. At some point in time and space, I get an element and want to treat it as an object of a subclass.
involves:
class subClassA : mainclass class subClassB : mainclass
Now I have a question, if you ask which class it is:
if(someObject is subClassA) { subClassA aA = someObject as subClassA ; }
While the correct object is returned for most subclasses, I get a null -Object for one subclass.
If I do the following:
if(someObject is subClassA) { subClassA aA = someObject as subClassA ; // <- aA = null; someObject is shown in debugger as object of subClassA object test = someObject as subClassA; // <- object is not null // or subClassA aB = (subClassA)someObject; // <- object is not null, shows the correct object }
I have results in test and aB .
What? I do not understand:
Why does as fail and prefix succeed?
Now I am completely lost.
if(someObject is subClassA) { subClassA aA = someObject as subClassA ; // <- aA = null; someObject is shown in debugger as object of subClassA subClassA aB = someObject as subClassA ; // <- aB != null. } if(someObject is subClassA) { subClassA aB = someObject as subClassA ; // <- aB != null. }
The name aA is defined locally. Only one thread accesses a method. If I just rename aA, it works.