views:

81

answers:

2

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.

A: 

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

John Fisher
So what should I edit within the ClLibApp? Could you please explain more about your solution? I am unclear about it.
We still don't know exactly what you're trying to achieve, so by definition our answers will be unclear, vague suggestions.
John Fisher
+1  A: 

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;
WayneC
Hi there, I understand that this is only applicable to methods that have the same name (in our case it's Subtract). But what if I have different methods names, what should I do?
This is "sort-of" an implementation of the strategy pattern. The method name could be the same but perform different tasks. For example the method could have just been "PerformOperation" and depending on the class implementing the interface, the "operation" could be Add, Subtract, Multiply, etc. See here for a good C# Strategy Pattern example ==> http://www.c-sharpcorner.com/UploadFile/rmcochran/strategyPattern08072006095804AM/strategyPattern.aspx
WayneC