Easy question for a c# newb



  • Disclaimer: I'm a self-taught programmer, and I use the term loosely. I'm slowly learning the OOP thing, after years of Perl and PHP scripting.

    So, I got bored the other day and decided to play with C# a little, specifically ASP webservices. I'm writing My First Web Service by Kenner, and already running into a problem:

    "TestService.Service1.FieldStaff cannot be serialized because it does not have
    a default public constructor."


    My code:


            public class FieldStaff {
                public FieldStaff(string UserName) {
                    this.UserName = UserName;
                }
                public string UserName;
            }

            [WebMethod]
            public FieldStaff GetFieldStaff() {
                FieldStaff testObj = new FieldStaff("John Smith");
                return testObj;
            }



    What dumbass thing am I not doing here?



  • public FieldStaff () {
      this.UserName = "some default value"; // null is a good choice
    }

    "default constructor"s are constructors which take no arguments.



  • I knew it was something simple. And so:


            public class FieldStaff {
                public FieldStaff() : this(null) {}
                public FieldStaff(string UserName) {
                    this.UserName = UserName;
                }
                public string UserName;
            }

    Works beautifully. Thanks much.



  • The error says that you need a default constructor, which is one that takes no arguments:


    public class FieldStaff 
    {
    // -------------------------------------------
    // public member variable
    // -------------------------------------------
    public string UserName;

    #region Constructors
    // -------------------------------------------
    // default constructor, required by Serialization
    // -------------------------------------------
    public FieldStaff()
    {}

    // -------------------------------------------
    // regularly-used constructor
    // -------------------------------------------
    public FieldStaff(string UserName)
    {
            this.UserName = UserName;
            }
    #endregion
    }


  • For serialization you might be able to have the default constructor private (serialization is a special case as it uses reflection to find the constructor, which can locate and call private constructors).



  • From the error message, I would say you need to add two things:

    1) A public constructor with no parameters that sets the username to a sensible default (null?)
    2) Accessors for username so that it can be get/set outside of the constructor

    The reason you are getting the error is that deserialisation requires an 'empty' object to exist so that it can populate the members using accessors. The way it does this is to create an object by passing no parameters to the constructor. In this case, it can't because such a constructor does not exist.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.