-
January 13th, 2014, 08:14 AM
#1
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 ?
-
January 13th, 2014, 02:06 PM
#2
Re: struct initialization
-
January 14th, 2014, 02:09 AM
#3
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 ?
-
January 14th, 2014, 12:25 PM
#4
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.
-
January 14th, 2014, 02:31 PM
#5
Re: struct initialization
Btw, you can get rid of this error by calling : this() in the constructor.
Code:
public FullName(string First)
: this()
{
...
}
-
January 14th, 2014, 02:56 PM
#6
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 !
-
January 14th, 2014, 03:03 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|