Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with the definition of this Struct type

Tags:

c#

struct

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

like image 889
Bohn Avatar asked Oct 11 '11 19:10

Bohn


People also ask

What is struct type?

A structure type (or struct type) is a value type that can encapsulate data and related functionality. You use the struct keyword to define a structure type: C# Copy.

How do I know the type of struct?

You can use the reflect package to find the given type of a struct. Reflection package allows determining the variables' type at runtime. Method reflect. TypeOf returns main.

Why is my struct an incomplete type?

An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An incomplete type can be: A structure type whose members you have not yet specified. A union type whose members you have not yet specified.

What does struct mean in C++?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, string, bool, etc.).


2 Answers

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; }
}
like image 79
Matthew Abbott Avatar answered Oct 13 '22 19:10

Matthew Abbott


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.

like image 22
n8wrl Avatar answered Oct 13 '22 18:10

n8wrl