C# multiple class library with common base class/namespace problem

cnamespaces

I'm new to c# and I'm trying to figure out if I can create multiple derived class libraries that each represent a specific item. I'll call the two class libraries ClassA & ClassB. Each class will share a BaseClass (namespace BaseNS).

I then created a c# app that refrences both ClassA and ClassB. This generated an error because they both have the same BaseClass. No problem, I used 'Alias' on the reference. This worked fine. So I have:

extern alias Class1Alias;
extern alias Class2Alias;

Here is where I am running into a problem.

I want to make a function that takes BaseClass as an argument.
public void SetData(BaseClass bc) { }
Can't do this because BaseClass isn't defined because of the alias. So it has to be:
public void SetData(Class1Alias.BaseNameSpace.BaseClass bc) {}

This works, but now I want to do the following:

Class1Alias.Class1Space.Class1 c1 = new Class1Alias.Class1Space.Class1();
Class2Alias.Class2Space.Class2 c2 = new Class2Alias.Class2Space.Class2();

this works

Class1Alias.BaseNameSpace.BaseClass bc = (Class1Alias.BaseNameSpace.BaseClass)c1;
SetData(bc);

this doesn't

Class1Alias.BaseNameSpace.BaseClass bc = (Class1Alias.BaseNameSpace.BaseClass)c2;
SetData(bc);

I tried casting it to Class2Alias, which works, but then I can't call my SetData function because it wants a Class1Alias.

Is there anyway to work around this?

Best Answer

Your terminology is somewhat confusing. You refer to "class libraries" in a way that makes me think you mean just "class", but I'm not sure. For example "class libraries" cannot be "derived" from a base class.

You said you are new to C#. Is there another language you are more familiar with? Maybe if you could express what you want to do in terms of that language, maybe someone can translate it to C#.

Related Topic