Bohn
Bohn

Reputation: 26919

What is wrong with the definition of this Struct type

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

Upvotes: 6

Views: 673

Answers (5)

n8wrl
n8wrl

Reputation: 19765

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.

Upvotes: 6

KevinDTimm
KevinDTimm

Reputation: 14376

You could also call the default constructor:

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

See here

Upvotes: 2

Matthew Abbott
Matthew Abbott

Reputation: 61589

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

Upvotes: 6

Mike Cowan
Mike Cowan

Reputation: 919

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

Upvotes: 3

CodingGorilla
CodingGorilla

Reputation: 19842

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.

Upvotes: 2

Related Questions