Hi there. I have 2 apps: Class Library named ClLib, and Windows Form Application called ClLibApp.
In ClLib, I have a group of classes.
They are as shown below:
Singapore (parent class)
- Nursery
- Primary
- Secondary
In ClLibApp, I need to add a reference from ClLib, so that I can use my application. The codings will be shown below.
Singapore
namespace ClLib.Singapore
{
public class SingaporeClass
{
public int subtract(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
}
Nursery
namespace ClLib.Singapore.Nursery
{
public class ExchangeClass
{
public int subtractionNursery(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
}
Primary
namespace ClLib.Singapore.Primary
{
public class ExchangeClass
{
public int subtractPrimary(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
}
Secondary
namespace ClLib.Singapore.Secondary
{
public class ExchangeClass
{
public int subtractSecondary(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
}
I do not want to put the methods all in the same class, meaning that I will want to have 3 different subclasses instead of having only 1 sub class to contain all the methods.
So in my ClLibApp, I create a button, and needs to have a directive that allows me to show the following:
using ClLib.Singapore;
using ClLib.Singapore.Nursery;
using ClLib.Singapore.Primary;
using ClLib.Singapore.Secondary;
Take for instance, I have created a button called btnExchange, and it will show the answers for different methods. I would like to create it in a way somehow like this:
private void btnExchange_Click(object sender, EventArgs e)
{
ExchangeClass ExchClass = new ExchangeClass();
string answer = ExchClass.subtract(99,88).ToString();
MessageBox.Show(answer);
}
In the second line, I want to be able to use
string answer = Exch.subtractNursery(100,694).ToString();
string answer = Exch.subtractPrimary(8484,38).ToString();
string answer = Exch.subtractSecondary(39, 764).ToString();
I need guidance on this, and I have been trying to solve this for a few days but to no avail.

1 answers
As you have three classes called ExchangeClass, all of them in different namespaces which you're importing from the dll, the C# compiler has no idea which one you mean to instantiate and so will throw an ambiguity error.
To resolve this, you're therefore going to have to fully qualify each of the ExchangeClass's with its namespace before you can call the appropriate method. For example:
If you want to instantiate different classes and call their methods when btnExchange is clicked, then you're going to need some way of determining which one to call - perhaps a set of RadioButtons (only one can be checked at a time) or something like that.
Incidentally, there isn't any inheritance relationship between the classes, so I don't understand why you're talking about parent class and subclasses? Another thing which is puzzling me is that all the methods do exactly the same thing?
answered 2 years ago by:
17279