C# methods & namespace problem
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.
What about something like this using generics:
In your CILib assembly:
public interface IExchangeClass
{
int Subtract(int firstNum, int secNum);
}
public class SingaporeClass : IExchangeClass
{
public int Subtract(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
public class NurseryClass : IExchangeClass
{
public int Subtract(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
public class PrimaryClass : IExchangeClass
{
public int Subtract(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
public class SecondaryClass : IExchangeClass
{
public int Subtract(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
public class ExchangeHelper
{
public int Subtract<T>(int firstNum, int secNum) where T : IExchangeClass, new ()
{
T exchange = new T();
return exchange.Subtract(firstNum, secNum);
}
}
Then in your ClLibApp you could do something like this:
ExchangeHelper helper = new ExchangeHelper();
string answer = helper.Subtract<SingaporeClass>(10, 4).ToString();
// or string answer = helper.Subtract<NurseryClass>(10, 4).ToString();
// or string answer = helper.Subtract<PrimaryClass>(10, 4).ToString();
// or string answer = helper.Subtract<SecondaryClass>(10, 4).ToString();
return answer;
There are many possible solutions, but I don't see how you plan to choose the correct line for the answer.
You could use extension methods:
namespace ClLib.Singapore.Primary
{
public static class ExchangeExtensions
{
public static int subtractPrimary(ClLib.Singapore.SingaporeClass @this, int firstNum, int secNum)
{
return firstNum - secNum;
}
}
}
Or, you could use general object-oriented design ideas. There are all sorts of tutorials on this sort of thing, if you look around on the web. You would want either interface inheritance or class inheritance. (With class inheritance, I would assume that SingaporeClass is the base and the others are subclasses.)
If neither of those solves your needs, Reflection may be the answer you need. There are many ways it can be used, so you'll need to describe your goal more precisely. (Maybe write some pseudo-code into the question.)
精彩评论