-1

I'm having trouble implementing the Factory Design Pattern. I've looked at http://dotnet.dzone.com/articles/design-patterns-c-factory, which follows my design very closely since it was the first good example I found a while back.
I took a look at Abstract Factory Design Pattern, but my design is very different and I'm having trouble deciding if I need that.

Right now, this is what it looks like. It used to be more simple, with my CR5, interface, factory, CB_spec, El, etc in the same visual studio project. I had to move things around as shown per design discussions and the need for CR6, etc to be separate. Now I'm getting some compilation problems I'm not sure what to do with. See ** below. My question is regarding the two compilation issues below.

My iCR Visual Studio project:

    public interface iCR
    {
        int CB_IO_Init(int slaveIndex);
        int WritePortReady();
        int WritePortBusy();
        void initCRData(byte[] writeBuffer, byte[] statusBuffer, int SlaveIndex, USB_Comm.CB cb, int cr_Type);
        int ProcessTWriting(ref Byte[] writeDat, ref Byte[] statusDat, ref Byte[] dataDumpWriteCheck);
        void Failure(String message);
        void Success(String message);

    }

My CR_Factory Visual Studio project

namespace CR_Factory
{

public class Cr
{

}  

public class CRFactory
{
    public enum CRType
    {
        CR0,
        CR1,
        CR3,
        CR4,
        CR5,
        CR6
    }

    public CRFactory()
    {
    }

    public iCR GetCR(CRType type) 
    {
        iCR cr = null;
        switch (type)
        {
            case CRType.CR5:
                cr = new CR5(); //**compile error..Cannot implicitly convert type ‘CR5’ to iCR’. An explicit conversion exists (are you missing a cast?)
                break;
            case CRType.CR6:
                //not done yet
                break;
            default:
                throw new ArgumentException(string.Format("A CR of type {0} cannot be found", Enum.GetName(typeof(CRType), type)));
        }
        return cr;
    }

    public CRType DetermineCR_Type(int type)
    {
        switch (type)
        {
            case 0:
                return CRType.CR0;
            //break;
            case 1:
                return CRType.CR1;
            case 3:
                return CRType.CR3;
            case 4:
                return CRType.CR4;
            case 5:
                return CRType.CR5;
            case 6:
                return CRType.CR6;
            default:
                throw new ArgumentException(string.Format("A type of type {0} cannot be found", type));

        }
    }

    }
}

My CR5 Visual Studio Project has a lot of classes in it, but right now I’m just showing you the part referred to in the factory. Later I’ll create a CR6 VS project, etc.:

public class CR5 : iCR
{        


    CB_703 cb_specific = null;

    //constructor
    public CR5()
    {
        cb_specific = new CB_703(SlaveIndex);
    }


    public int CB_IO_Init(int SlaveIndex)
    {
        int result = -534;
        result = cb_specific.IO_Init(SlaveIndex);
        return result;
    }
.
.
.
}

I have another Visual Studio Project (actually several) that instantiates the factory and gets the appropriate type. We’ll call it El:

namespace CrWr
{

public partial class PControl : UserControl
{
    //setup 

    //constructor
    public PControl()
    {

    }

    /// <summary>
    /// Get the P Control for chosen dll
    /// </summary>
    public Control GetPControl(USB_Comm.CB cbInstance, string dllSelected, THandlerApplication.Temp.TEMP[] temp, string dll, SC.SC.S_C c0)
    {
        cb = cbInstance;
        createControls();
        itsDll = dll;
        tArr = temp;
        cert = c0;

        CR_Factory.CRFactory factory = new CR_Factory.CRFactory();
        CRFactory.CRType type = factory.DetermineCR_Type(cr_Type);
        try
        {
            cr = factory.GetCR(type); //**compile error GetCR is not supported by the language
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException);
        }
        return this;
    }

private void OnP()
    {
        int result = -536;

        while (rL)
        {
            result = cr.CB_IO_Init(SlaveIndex); 
            if (result == 0)
            {
                …
            }
        }

.
.
.
}
Community
  • 1
  • 1
Michele
  • 3,617
  • 12
  • 47
  • 81
  • 2
    Please reduce the code to the parts necessary to understand your problem. Especially the `using` statements and the comments only clutter the listings. Probably nobody will take the time to read your code if you don't make it readable in the first place. – Dennis Traub Nov 15 '12 at 21:19
  • So.....what's the question here?? – D3vtr0n Nov 15 '12 at 21:19
  • 2
    Oh, and it's probably better suited for http://codereview.stackexchange.com/ anyway – Dennis Traub Nov 15 '12 at 21:21
  • The question, as stated in the first couple of paragraphs, is "Now I'm getting some compilation problems I'm not sure what to do with." This is since I split my project out into more VS projects to get ready for my CR6 implementation. Does anyone have any suggestions about the errors I'm seeing? See **. – Michele Nov 16 '12 at 13:08
  • I got rid of the using statements. I wasn't sure if the issue was with the Factory implementation, so that's why I'm showing so much code. – Michele Nov 16 '12 at 17:46

2 Answers2

1

My guess would be that your various projects are referencing different versions of the .Net runtime or platform profiles. My suspect would be your CR5 project is the one to check. The factory is returning an object that the target framework for the consumer cannot use. (I.e. CR5 is .Net 4 while the Factory/Consumer are .Net 3.5/2 or ClientProfile -though that last one may work, I don't really mess with different project types.)

Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • Thanks for the suggestion, Steve Py. I know CR5 is an older version than my El and other VS projects. It hasn't been a problem so far (before I split out into different VS projects more to get ready to do CR6 in the Factory), but maybe it doesn't work when it's split out this way. I'll give it a try. – Michele Nov 16 '12 at 13:06
0

The problem wound up being that I had a reference to the same class in both my interface parameter and also the CR5 project. Since it was circular, it was causing weird compilation errors. This happened when I moved things around to get ready for the factory to use the CR6 class.

Michele
  • 3,617
  • 12
  • 47
  • 81