开发者

What is wrong with the definition of this Struct type

开发者 https://www.devze.com 2023-04-12 20:41 出处:网络
I have defined my struct like this: struct Test { private string assayName; public string AssayName { get; set; }

I have defined my struct like this:

struct Test
{
    private string assayName;
    public string AssayName { get; set; }

    private string oldUnitName;
    public string OldUnitName { get; set; }

    private string new开发者_Python百科UnitName;
    public string NewUnitName { get; set; }

    public Test(string name, string oldValue, string newValue)
    {
        assayName = name;
        oldUnitName = oldValue;
        newUnitName = newValue;
    }

}

but it gives me the following error:

"Error 13 Backing field for automatically implemented property 'EnterResults.frmApplication.Test.NewUnitName' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer."


Well, there are two issues really:

1.Your using automatic properties, but then also providing fields, there is no wiring between the two.

2.When you use automatic properties, because this is a struct, they have to be initialised first. You can do this with a call to the default constructor. So a revised version would be:

struct Test
{
    public Test(string name, string oldValue, string newValue)
        : this()
    {
        AssayName = name;
        OldUnitName = oldValue;
        NewUnitName = newValue;
    }

    public string AssayName { get; private set; }
    public string OldUnitValue { get; private set; }
    public string NewUnitValue { get; private set; }
}


You aren't actually doing anything with the properties. Try this:

struct Test 
{ 
    public string AssayName { get; set; } 
    public string OldUnitName { get; set; } 
    public string NewUnitName { get; set; } 

    public Test(string name, string oldValue, string newValue) : this()
    { 
        AssayName = name; 
        OldUnitName = oldValue; 
        NewUnitName = newValue; 
    } 
} 

I think this has to do with struct initialization. Note the call to the default constructor I added seems to make it happy :)

"Seems to make it happy" - how dumb is that. I poked around for the real answer which is tied to how structs are initialized. Calling the default constructor insures fields are initialized before the struct is used.


You can remove the private fields assayName, oldUnitName and newUnitName. You then refer to the automatically implemented properties in your constructor:

public Test(string name, string oldValue, string newValue)
{
    AssayName = name;
    OldUnitName = oldValue;
    NewUnitName = newValue;
}


You are attempting to create an Automatically Implemented Property, but you're defining "Backing fields" (which have no apparent use), and then you're assigning values to those backing fields in your constructor and leaving your properties completely untouched.


You could also call the default constructor:

public Test(string name, string oldValue, string newValue) : this() 
{
   assayName = name;
   oldUnitName = oldValue;
   newUnitName = newValue;
}

See here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号