David Green
David Green

Reputation: 1234

C# Casting Not Working

I have these classes, partial listing of the relevant bits.

public class IVRTopology {}

public abstract class SANSwitch 
{  public string name { get; set; }
}

public class CiscoSwitch : SANSwitch
{
  public IVRTopology IVRTop = new IVRToplogy()
}

 class SwitchViewModel : INotifyPropertyChanged
{
   public SANSwitch sanswitch {  get;  set;  }
}

When I do something like this:

SwitchViewModel svm = new SwitchViewModel();
svm.sanswitch = new CiscoSwitch();
IVRTopology topo = svm.sanswitch.IVRTop;

the compiler warns me that svm.sanswitch is of type SANSwitch and doesn't have a definition for IVRTop if I try to access IVRTop thusly:

IVRTopology topo = (CiscoSwitch)svm.SANSwitch.IVRTop

doesn't work either. I don't want to add a definition for IVRTopology to the abstract class because I will have other subclasses of it that don't use it. Is there something wrong with the cast? How can I make this work? I want the view model to eventually support different kinds of switches.

Upvotes: 1

Views: 2371

Answers (3)

Mio Bambino
Mio Bambino

Reputation: 544

You're casting the result of svm.SANSwitch.IVRTop, when you actually want to cast svm.SANSWitch first like this:

((CiscoSwitch) svm.SANSwitch).IVRTop

Even better, in C# you have safe casting (since (CiscoSwitch) svm.SANSwitch might throw an error if it's of the incorrect type), you can do something fancy like this:

(svm.SANSwitch as CiscoSwitch)?.IVRTop

First we make a safe cast to CiscoSwitch, which might be null, then we Null Propagate ? and retrieve the value. Null Propagation only works if the resulting value is not null - so everything is pretty safe in this line. Then you might want to check if it succeeded in the first place.

Upvotes: 2

Tim Copenhaver
Tim Copenhaver

Reputation: 3302

Your syntax is wrong.

IVRTopology topo = ((CiscoSwitch)svm.SANSwitch).IVRTop;

Upvotes: 0

RB.
RB.

Reputation: 37172

Casting is a lower precedence operation than the . operator.

Therefore, when you run the following code:

IVRTopology topo = (CiscoSwitch)svm.SANSwitch.IVRTop

You are effectively running

var ivrTop = svm.SANSwitch.IVRTop;
IVRTopology topo = (CiscoSwitch)ivrTop;

The easy fix is to use brackets to force the correct resolution:

IVRTopology topo = ((CiscoSwitch)svm.SANSwitch).IVRTop

Upvotes: 5

Related Questions