开发者

Using an Inheriting Methods with a different variable (of a different type)

开发者 https://www.devze.com 2023-01-14 03:44 出处:网络
I am new to programming with OOP, I am trying to properly use the concept of inheritance. Here is the code here is what I tried (Link Here)

I am new to programming with OOP, I am trying to properly use the concept of inheritance.

Here is the code here is what I tried (Link Here)

public class Base
{开发者_运维问答
    //Type?
    public abstract static object Adapter();              

    public static DataTable GetWOCost(DateTime Date, string WO)
    {
        Application.UseWaitCursor = true;
        DataTable dt = new DataTable();

        try
        {
            //Cast?
            dt = Adapter().GetDataByWO(WO, Date);
            Application.UseWaitCursor = false;
            return dt;

        } catch (Exception)
        {
            return null;
        }
    }
} 

public class Materiel : Base
    {
        static AllieesDBTableAdapters.CoutMatTableAdapter Adapter()
        {
            return new AllieesDBTableAdapters.CoutMatTableAdapter();        
        }
    }

    public class Labor : Base
    {
        static AllieesDBTableAdapters.CoutLaborTableAdapter Adapter()
        {
            return new AllieesDBTableAdapters.CoutLaborTableAdapter();
        }
    }

At first all of my code was in the Material class. But I then had to add a second identical class, but for a different SQL adapter. I tried different things, but the above code has a big problem.

Since the type is changing I used object, but it will not work without a cast. But since I cannot know what type it will be, what is the proper way for having 2 or more class that has the methods GetWOCost, but with different adapters?

Maybe I should change to .NET 4.0 and use a dynamic object?

edit: Also there seems to have a problem with abstract and static, so I cannot use the static modifier on my Method GetWOCost() without having an instance of Adapter() (in the base class). It seems it would be easier to just copy-paste, but I am trying to figure out the proper way to do it.


What you should do in this kind of situation, is to program against an interface, which defines the contract for what you want to do with the Adapter object. If your implementation classes do not share a common interface, you can create one for them.

Then you can create different implementations of this interface; and in each subclass override the adapter method to return the correct interface implementation.

You should get rid of your static methods, since the whole idea of polymorphic types is that you can get different instances that does the same thing, but in different ways.

In code, this might look like this (simplified):

public interface IAdapter 
{
    DataTable GetWOCost();  // Implementors must have a method with this signature
}

public class Base 
{
    public abstract IAdapter Adapter();  

    // Methods that use IAdapter instances here.
}

class Materiel : Base 
{
    public override IAdapter Adapter() { return new CoutMatTableAdapter(); } 
}

class Labor : Base 
{
    public override IAdapter Adapter() { return new CoutLaborTableAdapter(); }
}

Then you just need to have your different adapters implement your new interface, such as:

public class CoutLaborTableAdapter : IAdapter 
{
    public DataTable GetWOCost() { /* implementation */ }
}


OK I did make your solution work, there was a little bit of confusion with the GetDataByWO & GetWOCost. You did not use the right one.

    public interface IAdapter
{
    DataTable GetDataByWO(string WO, DateTime Date);   
}

public abstract class Base
{
    public abstract IAdapter Adapter(); 

    public DataTable GetWOCost(DateTime Date, string WO)
    {
        Application.UseWaitCursor = true;
        DataTable dt = new DataTable();

        try
        {
            dt = Adapter().GetDataByWO(WO, Date);
            Application.UseWaitCursor = false;
            return dt;

        } catch (Exception)
        {
           return null;
        }
    }
}

public class Materiel : Base
{
    public override IAdapter Adapter()
    {
        return new CoutMatTableAdapter();        
    }
}

public class Labor : Base
{
    public override IAdapter Adapter()
    {
        return new CoutLaborTableAdapter();
    }
}

public class CoutMatTableAdapter: IAdapter
{
    public DataTable GetDataByWO(string WO, DateTime Date)
    {
        AllieesDBTableAdapters.CoutMatTableAdapter adpt = new AllieesDBTableAdapters.CoutMatTableAdapter();
        return adpt.GetDataByWO(WO, Date);
    }
}

public class CoutLaborTableAdapter : IAdapter
{
    public DataTable GetDataByWO(string WO, DateTime Date)
    {
        AllieesDBTableAdapters.CoutLaborTableAdapter adpt = new AllieesDBTableAdapters.CoutLaborTableAdapter();
        return adpt.GetDataByWO(WO, Date);
    }
}

Now to find out why the labor class throws an exception (the SQL part generated). So it seems that Abstract and static don't go well together (I can understand why, but it would have been great to have my method static)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号