Classes with Subclass properties and method accessability

KHerron

New member
Joined
Aug 18, 2016
Messages
1
Programming Experience
5-10
Hello,

I am new to your forum, and thought I would try my question here as I see this is a very active forum with honest answers!
Not very sure I worded my question correctly, and I'm not too sure I really know how to ask it...:shame:

So I will ask by demonstration. See below:

I have the following 3 classes.
C#:
public class A
{
    public string Name { get; set; }
    public string Address { get; set; }
    public List<B> Commands { get; set; }
    public C Api = new C();
}

public class B
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class C
{
    public int Port { get; set; }

    public void SendCommand(A Parent, B Command)
    {
        APISend(Parent.Address, Command.Value, this.Port)
    }
}
So Now, in my application, I use the following:
C#:
class Program
{
    static void Main(string[] args)
    {
        A MyNewDevice = new A();
        Populate(MyNewDevice);  // retrieves and sets the Name and Address Properties
        GetCommands(MyNewDevice);   // retrieves and populates the List of Commands
        MyNewDevice.Api.Port = 8080;
        MyNewDevice.Api.SendCommand(MyNewDevice, MyNewDevice.Commands[1]);
    }
}
This seems very redundant to me...

Is there a way I could simplify so that Class C could get the properties from the parent class.
For Example:
C#:
public class C
{
    public int Port { get; set; }

    public void SendCommand( B Command)
    {
        APISend(A.Address, Command.Value, this.Port)
    }
}
C#:
MyNewDevice.Api.SendCommand(Commands[1]);
 
No, C doesn't know about A. You could add a SendCommand in A that calls C.SendCommand and passes itself as argument, or add a 'parent' property in C so that it would know about it.
 
Back
Top Bottom