C# Inheritance Issue

Looking for a multiple inheritance workaround

Page 1 of 1

3 Replies - 946 Views - Last Post: 13 January 2010 - 10:25 AM Rate Topic: -----

#1 stv813  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 31-March 09

C# Inheritance Issue

Post icon  Posted 13 January 2010 - 09:45 AM

I have a program that's doing work on a comm port (through USB). Each device it works with extends an abstract class that contains code for common commands. I can't post the real code, but here's an equivalent of what's I have

	public abstract class CommTalker
	{
		public virtual int GetDeviceID()
		{
			// do comm
		}
	}

		public class Device1 : CommTalker
	{
		public virtual int GetSomething()
		{
			// do comm
		}
		public virtual void SetSomething(int value)
		{
			// do comm
		}
	}

	public class Device2 : CommTalker
	{
		public virtual int GetInfo()
		{
			// do comm
		}
		public virtual void SetInfo(int value)
		{
			// do comm
		}
	}




I need to make a simulator for each device that overrides each command so that no comm port communication is done. The problem I have is that each simulator must extend the Device class, but this means I have to do overrides for each function in the base CommTalker class for each simulator.

		public class Device1Sim : Device1 
	{
		public override int GetDeviceID()
		{
			return 234534;
		}

		int something = 5;
		public override  int GetSomething()
		{
			return something;
		}
		public override  void SetSomething(int value)
		{
			something = value;
		}
	}

	public class Device2Sim : Device2
	{
		public override  int GetDeviceID()
		{
			return 234534;
		}

		int info = 6;
		public override  int GetInfo()
		{
			return info;
		}
		public override  void SetInfo(int value)
		{
			info = value;
		}
	}



In this code, CommTalker only has 1 function but in the real code it has about 30, so what I'd like to do would be something like this (to avoid duplicate code):


		public class CommTalkerSim : CommTalker
	{
		public override int GetDeviceID()
		{
			return 234534;
		}
	}

		public class Device1Sim : Device1, CommTalkerSim
	{
		int something = 5;
		public override  int GetSomething()
		{
			return something;
		}
		public override  void SetSomething(int value)
		{
			something = value;
		}
	}

	public class Device2Sim : Device2, CommTalkerSim
	{
		int info = 6;
		public override  int GetInfo()
		{
			return info;
		}
		public override  void SetInfo(int value)
		{
			info = value;
		}
	}



I know C# doesn't support multiple inheritance and interfaces can't have code. Can anybody think of another way to do this that C# will allow?

Is This A Good Question/Topic? 0
  • +

Replies To: C# Inheritance Issue

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: C# Inheritance Issue

Posted 13 January 2010 - 10:15 AM

From what you posted it looks like the CommTalkSim class is the same for all of the classes related to Device Sim classes. Instead of inheriting the Device classes from CommTalker inherit them from CommTalkSim. CommTalkSim would have all of the methods of CommTalker available to it. So you would have this.

public abstract class CommTalker
{
}

public class CommTalkSim : CommTalker
{
}

public class Device1 : CommTalkSim
{
}

public class Device1Sim : Device1
{
}



It would be a case of using polymorphism instead of just inheritance. The CommTalkSim class has available all of what in CommTalker and you can access anything that is public in CommTalker through CommTalkSim.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: C# Inheritance Issue

Posted 13 January 2010 - 10:17 AM

You wrap it. You want the interfaces. If your code consists of a number of objects, hide them inside.

e.g.
public interface ICommTalker {
	int GetDeviceID();
}

public abstract class CommTalker : ICommTalker {
	public virtual int GetDeviceID() { /* do comm */ }
}

public interface IDevice1 : ICommTalker {
	int GetSomething();
	void SetSomething(int value);
}

public interface IDevice2 : ICommTalker {
	int GetInfo();
	void SetInfo(int value);
}


public class Device1 : CommTalker, IDevice1 { /* .. */ }
public class Device2 : CommTalker, IDevice2 { /* .. */ }

public class CommTalkerSim : CommTalker { /* .. */ }

public class Device1Sim : CommTalkerSim, IDevice1 { /* .. */ }
public class Device2Sim : CommTalkerSim, IDevice2 {
	private int info = 6;
	private Device2 dev2 = new Device2();
	
	public int GetInfo() { return dev2.GetInfo(); }
	public void SetInfo(int value) { dev2.SetInfo(value); }
}


Was This Post Helpful? 0
  • +
  • -

#4 stv813  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 31-March 09

Re: C# Inheritance Issue

Posted 13 January 2010 - 10:25 AM

View Postbaavgai, on 13 Jan, 2010 - 09:17 AM, said:

You wrap it. You want the interfaces. If your code consists of a number of objects, hide them inside.

e.g.
public interface ICommTalker {
	int GetDeviceID();
}

public abstract class CommTalker : ICommTalker {
	public virtual int GetDeviceID() { /* do comm */ }
}

public interface IDevice1 : ICommTalker {
	int GetSomething();
	void SetSomething(int value);
}

public interface IDevice2 : ICommTalker {
	int GetInfo();
	void SetInfo(int value);
}


public class Device1 : CommTalker, IDevice1 { /* .. */ }
public class Device2 : CommTalker, IDevice2 { /* .. */ }

public class CommTalkerSim : CommTalker { /* .. */ }

public class Device1Sim : CommTalkerSim, IDevice1 { /* .. */ }
public class Device2Sim : CommTalkerSim, IDevice2 {
	private int info = 6;
	private Device2 dev2 = new Device2();
	
	public int GetInfo() { return dev2.GetInfo(); }
	public void SetInfo(int value) { dev2.SetInfo(value); }
}




Ah yes. This will work. Thanks for the idea.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1