Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. no parameterless constructor defined for this object

no parameterless constructor defined for this object

Scheduled Pinned Locked Moved C#
questionhelp
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gilbert Consellado
    wrote on last edited by
    #1

    I have a class that has no parameters then i will create an object of it and convert it to stream and store it into a dictionary, every thing is fine until i add parameter to the class. basically i do this.

    var classObject = new ClassObject();
    dictionary.Add(id, new MemoryStream());
    ///then add the object stream here to the created storage on the dictioary

    then i will just retrieve it like this.

    var retriveObject = dictionary[id] as ClassObject;

    Now I add a single parameter on that class now using the last code above will produce

    Quote:

    no parameterless constructor defined for this object

    my question, is there way to fix the exception without removing the parameter i added to the class? Thanks

    OriginalGriffO L 2 Replies Last reply
    0
    • G Gilbert Consellado

      I have a class that has no parameters then i will create an object of it and convert it to stream and store it into a dictionary, every thing is fine until i add parameter to the class. basically i do this.

      var classObject = new ClassObject();
      dictionary.Add(id, new MemoryStream());
      ///then add the object stream here to the created storage on the dictioary

      then i will just retrieve it like this.

      var retriveObject = dictionary[id] as ClassObject;

      Now I add a single parameter on that class now using the last code above will produce

      Quote:

      no parameterless constructor defined for this object

      my question, is there way to fix the exception without removing the parameter i added to the class? Thanks

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Gilbert Consellado wrote:

      is there way to fix the exception without removing the parameter i added to the class

      Which implies that your code sample doesn't reflect the current code:

      var classObject = new ClassObject();

      Has been changed to something like

      var classObject = new ClassObject(myParameterValue);

      And the class constructor changed to something like

      public classObject(SomeClass parameterName)
      {
      ...

      As a result, the class no longer contains a parameterless constructor as the error message indicates. Create a new constructor:

      public classObject()
      {
      ...

      And it should get rid of the error.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      G 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Gilbert Consellado wrote:

        is there way to fix the exception without removing the parameter i added to the class

        Which implies that your code sample doesn't reflect the current code:

        var classObject = new ClassObject();

        Has been changed to something like

        var classObject = new ClassObject(myParameterValue);

        And the class constructor changed to something like

        public classObject(SomeClass parameterName)
        {
        ...

        As a result, the class no longer contains a parameterless constructor as the error message indicates. Create a new constructor:

        public classObject()
        {
        ...

        And it should get rid of the error.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        G Offline
        G Offline
        Gilbert Consellado
        wrote on last edited by
        #3

        Sorry, I think i didn't deliver my post right. The first example, that is the old code, and I already remove that. Actually the current code is something like this:

        var classObject = new ClassObject(parameterValue);
        dictionary.Add(id, new MemoryStream());
        ///then add the object stream here to the created storage on the dictionary

        Then I retrieve it like this.

        var retriveObject = dictionary[id] as ClassObject; //The runtime error while happen here

        Thanks

        OriginalGriffO 1 Reply Last reply
        0
        • G Gilbert Consellado

          Sorry, I think i didn't deliver my post right. The first example, that is the old code, and I already remove that. Actually the current code is something like this:

          var classObject = new ClassObject(parameterValue);
          dictionary.Add(id, new MemoryStream());
          ///then add the object stream here to the created storage on the dictionary

          Then I retrieve it like this.

          var retriveObject = dictionary[id] as ClassObject; //The runtime error while happen here

          Thanks

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          If I try to duplicate what you have:

          Dictionary<int, MemoryStream> dictionary = new Dictionary<int, MemoryStream>();
          var classObject = new ClassObject("hello");
          int id = 666;
          dictionary.Add(id, new MemoryStream());
          var retriveObject = dictionary\[id\] as ClassObject;
          }
          

          public class ClassObject : MemoryStream
          {
          public ClassObject(string s) { }
          }

          I don't get an error - I do get a null value, but that's what I expect. So how is my code different from yours?

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          G 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            If I try to duplicate what you have:

            Dictionary<int, MemoryStream> dictionary = new Dictionary<int, MemoryStream>();
            var classObject = new ClassObject("hello");
            int id = 666;
            dictionary.Add(id, new MemoryStream());
            var retriveObject = dictionary\[id\] as ClassObject;
            }
            

            public class ClassObject : MemoryStream
            {
            public ClassObject(string s) { }
            }

            I don't get an error - I do get a null value, but that's what I expect. So how is my code different from yours?

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            G Offline
            G Offline
            Gilbert Consellado
            wrote on last edited by
            #5

            hmmm, honestly I didn't know how to reply to this. It bothers me for a while. I apology, but are you serious with that code snippet or you just give me a sarcastic reply? ;) P.S. I didn't post the the whole code on may sample to make the post short.

            OriginalGriffO 1 Reply Last reply
            0
            • G Gilbert Consellado

              hmmm, honestly I didn't know how to reply to this. It bothers me for a while. I apology, but are you serious with that code snippet or you just give me a sarcastic reply? ;) P.S. I didn't post the the whole code on may sample to make the post short.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              No - I'm serious - I've just combined your code snippets into something that will compile and run to try and run down why you get the error in code that shouldn't give it. Obviously, I can't see code you haven't given us so I have to assume a minimum that gets it to compile - but I don;t expect it to match yours. It's there so you can explain where yours differs and to let me create a "closer" version for testing here.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              G 1 Reply Last reply
              0
              • G Gilbert Consellado

                I have a class that has no parameters then i will create an object of it and convert it to stream and store it into a dictionary, every thing is fine until i add parameter to the class. basically i do this.

                var classObject = new ClassObject();
                dictionary.Add(id, new MemoryStream());
                ///then add the object stream here to the created storage on the dictioary

                then i will just retrieve it like this.

                var retriveObject = dictionary[id] as ClassObject;

                Now I add a single parameter on that class now using the last code above will produce

                Quote:

                no parameterless constructor defined for this object

                my question, is there way to fix the exception without removing the parameter i added to the class? Thanks

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I'm positive you just need to add a parameterless constructor, EVEN if you don't intend to use it. You could always mark it internal too. I have to assume you're using an xml serializer because in my experience, this has always happened when an object lacked a parameterless constructor. I also have to assume that when you added a constructor with parameters, you removed the parameterless one. Can't do that...as a sidenote, it would be helpful to have explained how exactly you serialize the object for others to better understand your issue. So basically, it should look like this:

                [Serializable]
                public class ClassObject : MemoryStream
                {
                //Must be present for serialization to work!
                internal ClassObject() { }

                public ClassObject(string s) { }
                

                }

                G 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  No - I'm serious - I've just combined your code snippets into something that will compile and run to try and run down why you get the error in code that shouldn't give it. Obviously, I can't see code you haven't given us so I have to assume a minimum that gets it to compile - but I don;t expect it to match yours. It's there so you can explain where yours differs and to let me create a "closer" version for testing here.

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  G Offline
                  G Offline
                  Gilbert Consellado
                  wrote on last edited by
                  #8

                  Got it. :-D But i just decide to remove the parameter and just expose the property setter of one of its member, because it also introduce other code problem. btw, thanks

                  OriginalGriffO 1 Reply Last reply
                  0
                  • G Gilbert Consellado

                    Got it. :-D But i just decide to remove the parameter and just expose the property setter of one of its member, because it also introduce other code problem. btw, thanks

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    You're welcome!

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • L Lost User

                      I'm positive you just need to add a parameterless constructor, EVEN if you don't intend to use it. You could always mark it internal too. I have to assume you're using an xml serializer because in my experience, this has always happened when an object lacked a parameterless constructor. I also have to assume that when you added a constructor with parameters, you removed the parameterless one. Can't do that...as a sidenote, it would be helpful to have explained how exactly you serialize the object for others to better understand your issue. So basically, it should look like this:

                      [Serializable]
                      public class ClassObject : MemoryStream
                      {
                      //Must be present for serialization to work!
                      internal ClassObject() { }

                      public ClassObject(string s) { }
                      

                      }

                      G Offline
                      G Offline
                      Gilbert Consellado
                      wrote on last edited by
                      #10

                      Thanks, yeah it seems to work this way. But marking it internal will give the same error. Actually I use XtraReport a third party component it has SaveLayout and FromStream member. But i just decide to remove the constructor parameter and just pass the value through a property. P.S. Whooo my ISP giving me crap right now.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups