6
votes

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 newUnitName;
    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."

5
For starters: The compiler will not assume that assayName is the backing field for AssayName...Dan J
Your properties don't access or mutate the backing fields.canon
On a side-note, you probably want to use a class, not a struct.Henk Holterman

5 Answers

6
votes

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; }
}
6
votes

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.

3
votes

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;
}
2
votes

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.

2
votes

You could also call the default constructor:

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

See here