2

How would you inherit from one class and have multiples of a single property from it Example

And here is the actual code (So Far)

class NetWorkInterface
{
    public double Vlan;
    public string Ip;
}

class Port : NetWorkInterface
{
    public double PortNumber;
}
public void test ()
{
    Port newport = new Port();
    newport.PortNumber = 7;
    newport.Vlan = 100
}

I want to be able to add multiple interfaces to one port

moi_meme
  • 9,180
  • 4
  • 44
  • 63
Ryan Caputo
  • 63
  • 1
  • 1
  • 7

3 Answers3

3

I don't think you should be using inheritance here.

If you want a Port to contain a collection of NetworkInterfaces then a better definition of Port might be:

class Port
{
    public double PortNumber;
    public NetworkInterface[] networkInterfaces;
}
Slappywag
  • 1,143
  • 1
  • 17
  • 27
  • yea I am thinking the same thing now, just saw a video on it now inheritance is stuck in my head, trying to hard to use it – Ryan Caputo Aug 19 '15 at 14:36
  • 1
    You need to remove the inheritance from your code sample, it doesn't make sense. – Ron Beyer Aug 19 '15 at 14:37
  • I would use it if I needed to say, create a backhaul Port, which is a port but has some extra details, so it would inherit from the base class port. – Ryan Caputo Aug 19 '15 at 14:39
  • I would change the name `Port` to something like `NetworkPortBinding`. – Aron Aug 19 '15 at 14:41
0

Something along the lines off

    public class Port
    {
        public double PortNumber { get; set; }
        public IEnumerable<NetworkInterface> NetworkInterfaceList { get; set; }
    }
Loofer
  • 6,841
  • 9
  • 61
  • 102
  • 1
    You add almost nothing to the existing answer by @Slappywag – Aron Aug 19 '15 at 14:41
  • Great minds I suppose. there was no answer as I composed my masterpiece. Using arrays is pretty oldskool though. Also public fields are less good than properties (encapsulation baby!) so I win. – Loofer Aug 19 '15 at 15:21
0

You can inherit one class per class (no multiple inheritance in C#), or implement multiple interfaces, or do both

public interface NetWorkInterface1
{
  double Vlan {get; set;} 
}

public interface NetWorkInterface2
{
   string Ip {get; set;}
}

class Port : A /* (inherit class before interface) port inherits A & A inherits B*/, NetWorkInterface1, NetWorkInterface2 /* implements interface 1 & 2*/
{
    public double PortNumber;
    public double Vlan {get; set;} 
    public string Ip {get; set;}
}

public class A : B /* A inherits B */
{
  public string AStuff; 
}   

public  class B
{
  public string BStuff;         
}


public void test ()
{
    Port newport = new Port();
    newport.PortNumber = 7;
    newport.Vlan = 100;
    newport.AStuff = "A"; 
    newport.BStuff = "B"; 
}
patrick
  • 16,091
  • 29
  • 100
  • 164
  • This doesn't answer his question, he isn't trying to implement multiple interfaces. He's attempting to implement a single interface multiple times. – Derrick Moeller Nov 04 '16 at 17:03