views:

1399

answers:

3

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?

A: 

I am no sure if I understand the question correctly. Does the base class exist in a single assembly or it's copied and pasted in both assemblies? If it's copied and pasted, there's not much you can do as the compiler considers them as two different classes.

Set the assembly reference alias in properties window of the specific referenced assembly. Assign something other than global to it. Then you can reference the type in that assembly with something like:

assemblyAlias::RootNamespace.OtherNamespace.Class
Mehrdad Afshari
Correct. I'm trying to use a shared base class definition for both derived class libraries. I'm coming to the same conclusion that this can't be done. Thanks.
bradak
+2  A: 

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#.

Daniel Pratt
No, I meant "class libraries". Both class libraries will have a shared base class.
bradak
Are you saying that you have two *classes* in separate class libraries that you want to derive from the same base class? If yes, this is indeed possible. It may be helpful to point out that two classes with exactly the same name, but defined in different libraries _are two different classes_.
Daniel Pratt
yes, I am realizing that is my problem. I had hoped to be able to cast to the common base class and use it.
bradak
What prevents you from adding a reference from "class library A" to "class library B" and having "Class2Alias.Class2Space.Class2" derive directly from "Class1Alias.BaseNameSpace.BaseClass"? Or move BaseClass to a separate class library, referenced by the two you already have?
Daniel Pratt
I tried you last suggestion. I moved the base class to a separate class library, and referenced this in the derived class libraries, and it works now. Thanks for help.
bradak
A: 

Options that spring to mind:

  1. Merge the two class libraries
  2. Move the base class (and any other shared interfaces/classes) into a third, common, class library that they can both reference
  3. Change the namespace to avoid clashing base class names (if they are not indeed the same class)
Rowland Shaw