1

I have the following class structure:

class structure

The user can select to work with SystemA or SystemB.

The following code is from the class that creates the main object:

public class MainViewModel()
{
    var project = new Project();
    project.PropAList = new List<IInterfaceA>();
    project.PropAList.Add(new SystemB());    
}

My problem is that i want to get to SystemB properties (like 'Date') from another class (ViewModel) that gets the 'project' object. But in this class i can see only the base properties (and IInterfaceA) of the object and not the objects that was implemented from the interfaces ("IsOpen' and 'Date'). For example: I have a method that sets the Date in some view model but i can't activate it because that i don't know if the user initiates SystemA or SystemB.

So i can't write for example:

project.PropAList[0].Date = "1.1.2000";

Thanks

gr1d3r
  • 99
  • 3
  • 13
  • `(project.PropAList[0] as SystemB).Date = "1.1.2000";` Although if that item is **not** a `SystemB` it will throw an exception, so consider a null check before the `.Date` assignment. – mjwills Dec 27 '17 at 12:53
  • I would suggest that you check for the type, with: ```if(project.PropAList[0].GetType() == typeof(SystemB)){ (project.PropAList[0] as SystemB).Date = "1.1.2000"; } else if (project.PropAList[0].GetType() == typeof(SystemA)){ // do the other thing } ``` – Headhunter Xamd Dec 27 '17 at 12:54
  • Consider using `is` rather than `GetType` @HeadhunterXamd . – mjwills Dec 27 '17 at 13:05
  • Thanks, I tested the classes for in interface and now it works. `(project.PropAList[0] as IInterfaceB).Date = "1.1.2000";` – gr1d3r Dec 27 '17 at 13:12
  • 1
    Possible duplicate of [Typecasting in C#](https://stackoverflow.com/questions/1339482/typecasting-in-c-sharp) – mjwills Dec 27 '17 at 21:19

0 Answers0