CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Question struct initialization

    Code:
    public struct FullName
    {
       public string FirstName;
       public string LastName;
    
       public FullName(string First)
       {
          this.FirstName = First;
          this.LastName = Demo();
       }
    
       public string Demo() { return "demo"; }
    }
    Gives me the following compile error
    The 'this' object cannot be used before all of its fields are assigned to
    If FullName were to be class instead of struct, I don't get compilation errors.

    Can anyone explain this to me please ?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: struct initialization

    You'd probably want to use a CLASS http://msdn.microsoft.com/en-us/library/x9afc042.aspx
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Re: struct initialization

    It's not a question wether to use struct or class.

    I'd just like to know why struct gives me a compile error and class doesn't ?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: struct initialization

    To find the reason, type the full error into a bing or google search, then read a few replies.

    from http://msdn.microsoft.com/en-us/library/w29h4276.aspx

    All fields in a struct have to be assigned by a constructor before the constructor can call a method in the struct.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: struct initialization

    Btw, you can get rid of this error by calling : this() in the constructor.

    Code:
    public FullName(string First)
      : this()
    {
     ...
    }

  6. #6
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Re: struct initialization

    I had already figured out the :this() solution

    Thanks for your explanation on the CS0188 , the specific struct behavior is clear to me now !

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: struct initialization

    Hopefully, the big tip from this post is this:

    To find the reason, type the full error into a bing or google search, then read a few replies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured