Micha Metzger
Micha Metzger

Reputation: 11

cast is null only in visual studio debugger on some computer

I have a really strange problem:

object aObject = LoadObject();
MessageBox.show(aObject.GetType().Name) //box shows "Action"
Action aAction = aObject as Action;

if i do that in debugging in code, aAction is null. if i make a breakpoint somewhere and write (aObject as Action).Id in direct input field of visual studio i get correct value (nothing is null).

if i run the exe from bin/debug folder, it works... the cast is done and aAction is not null.

What the hell is wrong here?

Upvotes: 1

Views: 111

Answers (2)

Micha Metzger
Micha Metzger

Reputation: 11

There is no namespace problem. For testing this i used the complete namespace in front of the type.

The object is definitly the type i want to cast. The code runs if I start the exe. The cast don't work only in debugging.

if i use Action aAction = aObject as Action; aAction is null. if I use Action aAction = (Action)aObject; the cast returns an Exception.

Again: the code works if I start the exe, and the cast works in direct console of visual studio if I set a breakpoint. Only in code in debugging it is null.

And only on one machine! We tested there with VS2013 and VS2015, same problem.

Upvotes: 0

Joe Enos
Joe Enos

Reputation: 40421

I'm going to go out on a limb and say that your Action is in a different namespace.

For example, maybe LoadObject returns a MyApp.Action, where your aObject as Action is attempting to cast as System.Action.

If so, the easiest fix is to say aObject as MyApp.Action. Or avoid naming a class Action in the first place, to avoid conflicting with stuff in the System namespace.

Upvotes: 1

Related Questions