locked
No parameterless constructor defined for this object - What dows it mean

    Question

  • Guys,

    I have read other blogs on the forums about this error, but I still have no idea what it means or how t fix it.

    My code is thus:

    FObject FSystem;
    FSystem = (FObject)Activator.CreateInstance(OFSystem.GetType(), false); // It breaks here with the comment "No parameterless constructor defined for this object"

    My FObject class looks like the following:


    public class FObject : Listener
        {
            public string Parameters;
            protected ParameterParser PParser;

            protected string DefaultParameters;
            protected ParameterParser DParser;

            protected string RealParameters;
            protected ParameterParser RParser;

            public FObject()
            {
            }

            protected void InitializeParameters(string pParameters, string pDefaultParameters)
            {
                Parameters = pParameters;
                RealParameters = pParameters;
                DefaultParameters = pDefaultParameters;

                RParser = new ParameterParser(RealParameters);

                PParser = new ParameterParser(Parameters);

                DParser = new ParameterParser(DefaultParameters);

                Parameters = DParser.Merge(PParser);
                PParser = new ParameterParser(Parameters);
            }

            public virtual void Init(string pParameters)
            {
            }

            public virtual bool Finished()
            {
                return true;
            }

            public override string ToString()
            {
                string t;

                t = Title();

                return t;
            }

            public virtual string Title()
            {
                return "Basic FObject class";
            }

        }

    public class Listener : TListener
        {
            public virtual void ReceiveCandle(sCandle pCandle, int pPeriod, string pCBTitle)
            {
            }
        }

    public class TSystems
        {
            static private System.Collections.ArrayList FObjects;

            static TSystems()
            {
                FObjects = new System.Collections.ArrayList();

                           
                FObjects.Add(new fChange(FLAB.LabGUI.instrumentSelection,SamplingType.Ticks,FLAB.LabGUI.freq, FLAB.LabGUI.htInstrumentSelection));                            FObjects.Add(new fRandomExit());

             }

            static public System.Collections.ArrayList GetSystems()
            {
                return FObjects;
            }
        }

    Could someone please kindly explain to me why I have this error?

    Many thanks,
    Harry
    • Edited by HarryBedi Wednesday, September 09, 2009 10:32 PM
    Wednesday, September 09, 2009 10:17 PM

Answers

  • All disaster over - i have fgured it out.  The key word was "parameterless".

    The problem lay in me calling the FObjecs in TSystems

    public class TSystems
        {
            static private System.Collections.ArrayList FObjects;

            static TSystems()
            {
                FObjects = new System.Collections.ArrayList();

                           
                FObjects.Add(new fChange(FLAB.LabGUI.instrumentSelection,SamplingType.Ticks,FLAB.LabGUI.freq, FLAB.LabGUI.htInstrumentSelection));                            FObjects.Add(new fRandomExit());

             }

            static public System.Collections.ArrayList GetSystems()
            {
                return FObjects;
            }
        }

    The objects them selves had parameters and in affect they should not have so I split them out and used seperate call to initialise these parameters on the creation of the object.

    I hope that makes sense!

    Thanks to one and all for your help!

    Best,
    Harry
    • Marked as answer by HarryBedi Thursday, September 10, 2009 3:42 PM
    Thursday, September 10, 2009 3:42 PM

All replies

  • FSystem = (FObject)Activator.CreateInstance(OFSystem.GetType(), false);

    What type is OFSystem?
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, September 09, 2009 10:25 PM
  • Hi,

    The error means what it says... whatever the 'type' defined by OFSystem is doesn't have a constructor that has no arguments, i.e

    public class OFSystem
    {
    public OFSystem()
    {
    }
    }

    What is OFSystem ? It isn't declared in the sample calling code... you declare FSystem but not OFSystem. You can't pass FSystem.GetType() to CreateInstance since it hasn't been assigned a value yet.

    Can you show us the declaration for OFSystem ?
    Wednesday, September 09, 2009 10:26 PM
  • Hello,
    What about the type OFSystem?
    I would venture to guess that 'invalid cast' might be the next error.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, September 09, 2009 10:28 PM
    Moderator
  • Hi,

    The error means what it says... whatever the 'type' defined by OFSystem is doesn't have a constructor that has no arguments, i.e

    public class OFSystem
    {
    public OFSystem()
    {
    }
    }

    What is OFSystem ? It isn't declared in the sample calling code... you declare FSystem but not OFSystem. You can't pass FSystem.GetType() to CreateInstance since it hasn't been assigned a value yet.

    Can you show us the declaration for OFSystem ?

    Hi Guys,

    FObject OFSystem;
            string Parameters;
            string AParameters;

            public SystemTester(FObject pOFSystem, string pParameters, string pAParameters)
            {
                OFSystem = pOFSystem;
                Parameters = pParameters;
                AParameters = pAParameters;
            }
    Still not sure I am doing wrong?!?!

    Thanks,
    Harry

    Wednesday, September 09, 2009 10:29 PM
  • FSystem = (FObject)Activator.CreateInstance(OFSystem.GetType(), false);

    What type is OFSystem?
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!

    Hi Guys,

    FObject OFSystem;
            string Parameters;
            string AParameters;

            public SystemTester(FObject pOFSystem, string pParameters, string pAParameters)
            {
                OFSystem = pOFSystem;
                Parameters = pParameters;
                AParameters = pAParameters;
            }
    Still not sure I am doing wrong?!?!

    Thanks,
    Harry
    • Edited by HarryBedi Wednesday, September 09, 2009 10:36 PM
    Wednesday, September 09, 2009 10:30 PM
  • Hi,

    Is it correct to assume that OFSystem is declared as an FObject ?

    Your second post shows it set to a parameter of type FObject (pOFSystem) but doesn't show the actual declaration for the field.

    Is that is the case, I can't explain the error since FObject does indeed appear to have a public constructor that accepts no arguments.

    I copied your code into a blank project and commented out any lines that referenced objects/classes not in the post, and it compiled and ran fine (VS2008). Is it possible some part of your solution needs a rebuild ?
    Wednesday, September 09, 2009 10:35 PM
  • Do you have a typo on line 2?

    FObject FSystem;
    FSystem = (FObject)Activator.CreateInstance(OFSystem.GetType(), false);

    or is OFSystem another field?

    If it refers to the field in line 1, then your code won't compile because the field has not yet been initialized.

    On the other hand, called using the typeof operator, the code compiles and runs happily.
    FSystem = (FObject)Activator.CreateInstance(typeof(FObject), false);


    EDIT: Just notice the additional code where you defined OFSystem. If OFSystem was initialized correctly you code should work since type FObject, from what I can see, does have a parameterless constructor.
    Wednesday, September 09, 2009 10:36 PM
  • Hi Guys - I reposted the code, There is no typo and it reads as follows:

    FObject OFSystem;
            string Parameters;
            string AParameters;

            public SystemTester(FObject pOFSystem, string pParameters, string pAParameters)
            {
                OFSystem = pOFSystem;
                Parameters = pParameters;
                AParameters = pAParameters;
            }

    Yort - I tried a clean rebuild but I still have the same error.

    Thanks,
    Harry

    Wednesday, September 09, 2009 10:42 PM
  • The CreateInstance method uses Reflection to instantiate the object.  When you do not provide one the compiler will provide one for you in IL.  The JIT compiler does not have this luxury, it requires a default parameterless constructor.

    Just add one, 3 lines of code.  That should fix it.  This can come up when you de-serialize objects, too.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, September 09, 2009 10:45 PM
    Moderator
  • Hmm, odd this runs for myself and Wole. Doesn't seem like the code needs changing because of that, but if a clean build didn't help I don't know what to suggest next.

    Which versions of VS and .Net are you using ?




    Wednesday, September 09, 2009 10:46 PM
  • The CreateInstance method uses Reflection to instantiate the object.  When you do not provide one the compiler will provide one for you in IL.  The JIT compiler does not have this luxury, it requires a default parameterless constructor.

    Just add one, 3 lines of code.  That should fix it.  This can come up when you de-serialize objects, too.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    That's the weird thing Rudy, he does have one.

    public class FObject : Listener
    {
        ....

        public FObject()
        {
        }


        ...
    }
    Wednesday, September 09, 2009 10:49 PM
  • Hi Guys - I reposted the code, There is no typo and it reads as follows:

    FObject OFSystem;
            string Parameters;
            string AParameters;

            public SystemTester(FObject pOFSystem, string pParameters, string pAParameters)
            {
                OFSystem = pOFSystem;
                Parameters = pParameters;
                AParameters = pAParameters;
            }

    Yort - I tried a clean rebuild but I still have the same error.

    Thanks,
    Harry



    No, he does not.  Not for OFSystem. 
    Watch out for invalid cast.  That might be next.

    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, September 09, 2009 11:27 PM
    Moderator
  • Hi Rudedog,

    OFSystem is supposed to be of type FObject... FObject has as public parameterless constuctor defined;

    public class FObject : Listener
        {
            public string Parameters;
            protected ParameterParser PParser;

            protected string DefaultParameters;
            protected ParameterParser DParser;

            protected string RealParameters;
            protected ParameterParser RParser;

            public FObject()
            {
            }


    As shown in his original post... which class are you saying needs a new constructor added ?

    SystemTester seems to be the calling class, not the one being instantiated via CreateInstance... unless we are all mis-reading the code ?

    Wednesday, September 09, 2009 11:33 PM
  • Walk the inheritance chain.  What about Listener?

    public class FObject : Listener

    I trust what the compiler tells me.
    It is far more likely that I have made a mistake than the compiler.

    Adding a default constructor to all classes is a best practice.

    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, September 09, 2009 11:44 PM
    Moderator
  • Hi,

    I considered that, and was going to (but didn't suggest) adding a constructor to it. The reason I didn't suggest it is because his code works fine on my PC even without adding a public constructor to listener, and the code I compiled and ran has the same inheritance chain. 

    I guess it's worth a shot though.

    Harry,

    Try adding

    public Listener()
    {
    }

    to the Listener class as Rudedog suggests.

    Rudedog, can you explain why two of us can run the code succesfully without adding a parameterless constructor to the Listener class ?
    Wednesday, September 09, 2009 11:47 PM
  • One explanation is that your  TListener class has no constructors defined at all, but the OP's class has constructor that have constructors that require parameters but no parameterless constructor.
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, September 09, 2009 11:51 PM
    Moderator
  • Harry, can you try printing out the value of OFSystem.GetType().ToString() to see if it actually does match FObject. Might give us a clue.
    Wednesday, September 09, 2009 11:59 PM
  • True, we are all running the assumption that he is running the code he actually posted.
    I guess we'll wait to see what happens when he explicitly adds the constructor to listener.

    Thanks.
    Thursday, September 10, 2009 12:00 AM
  • The OP got the wrong assembly loaded, probably an old one.

    Hans Passant.
    Thursday, September 10, 2009 12:12 AM
    Moderator
  • Hi Hans,

    That's why I suggested he rebuild his solution, but he claims a 'clean build' didn't fix it.
    I guess he could check the dates and times on all the assemblies in the folder he's running from and make sure they're all 'recent'.
    Thursday, September 10, 2009 12:14 AM
  • The OP can browse the types in the Object Browser Window, set it for the current solution.  This will show the types defined for the currently loaded assemblies as Hans suggested.  Every type in the inheritance chain needs a public parameterless constructor all the way to System.Object.


    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, September 10, 2009 12:23 AM
    Moderator
  • Hi Ali - I tried that and they do match types of fChange.

    Thanks,
    Harry
    Thursday, September 10, 2009 7:36 AM
  • Guys,

    I have added the changes you have suggested and this still isnt working.  I can assure you that I have completed a clean build and that my assemblies are recent.  Just for clarity I am adding the code again with the changes I have made.

    public class SystemTester
        {
            FObject OFSystem;
            string Parameters;
            string AParameters;

            public SystemTester(FObject pOFSystem, string pParameters, string pAParameters)
            {
                OFSystem = pOFSystem;
                Parameters = pParameters;
                AParameters = pAParameters;
            }
            public void Execute()
            {
                   while (pp.GetOptimizationParameters(ref GUIParameters) && (Framework.ExecuteTransform == true))
                    {
                        FObject FSystem;
                        string s = OFSystem.GetType().ToString();
                        FSystem = (FObject)Activator.CreateInstance(OFSystem.GetType(), false);//WHERE THE CODE BREAKS

                      do{...................}
                    }
            }

    public class FObject : Listener
        {       
            public FObject()
            {
            }
        }

    public class TListener
        {
            public TListener()// I ADDED THIS CODE
            {
            }
            public virtual void Receive(sTick pTick, sQuote Quote, sPaidGiven PG)
            {
            }
        }

        public class Listener : TListener
        {
            public Listener()// I ADDED THIS CODE
            {
            }
            public virtual void ReceiveCandle(sCandle pCandle, int pPeriod, string pCBTitle)
            {
            }
        }
    public class TSystems
        {
            static private System.Collections.ArrayList FObjects;

            static TSystems()
            {
                FObjects = new System.Collections.ArrayList();

                           
                FObjects.Add(new fChange(FLAB.LabGUI.instrumentSelection,SamplingType.Ticks,FLAB.LabGUI.freq, FLAB.LabGUI.htInstrumentSelection));                            FObjects.Add(new fRandomExit());

             }

            static public System.Collections.ArrayList GetSystems()
            {
                return FObjects;
            }
        }

    Thanks,
    Harry
    Thursday, September 10, 2009 7:47 AM
  • Walk the inheritance chain.  What about Listener?

    public class FObject : Listener

    I trust what the compiler tells me.
    It is far more likely that I have made a mistake than the compiler.

    Adding a default constructor to all classes is a best practice.

    If Listener didn't have a parameterless constructor, the compiler would complain because of the call from the constructor of FObject to the constructor in its base class. Seems to me the OP is talking about a runtime error.
    Thursday, September 10, 2009 3:30 PM
  • All disaster over - i have fgured it out.  The key word was "parameterless".

    The problem lay in me calling the FObjecs in TSystems

    public class TSystems
        {
            static private System.Collections.ArrayList FObjects;

            static TSystems()
            {
                FObjects = new System.Collections.ArrayList();

                           
                FObjects.Add(new fChange(FLAB.LabGUI.instrumentSelection,SamplingType.Ticks,FLAB.LabGUI.freq, FLAB.LabGUI.htInstrumentSelection));                            FObjects.Add(new fRandomExit());

             }

            static public System.Collections.ArrayList GetSystems()
            {
                return FObjects;
            }
        }

    The objects them selves had parameters and in affect they should not have so I split them out and used seperate call to initialise these parameters on the creation of the object.

    I hope that makes sense!

    Thanks to one and all for your help!

    Best,
    Harry
    • Marked as answer by HarryBedi Thursday, September 10, 2009 3:42 PM
    Thursday, September 10, 2009 3:42 PM
  • http://msdn.microsoft.com/en-us/library/ms182275.aspx

    Static constructor checks can reduce performance.
    Thursday, September 10, 2009 4:12 PM