Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct in c# giving warning Field is never assigned to,and will always have its default value0

Tags:

c#

I have very simple struct as below:

 public struct ShFileInfo
 {
     public int hIcon;
     public int iIcon;
     public int dwAttributes;
 }

I have enabled warning as error. Now for all three int getting error

Field is never assigned to,and will always have its default value 0

Obviously I will get error if I try to initialize the int to 0. Is there any way to handle this without disabling warning as error.

like image 247
Shrivallabh Avatar asked Jul 30 '15 05:07

Shrivallabh


People also ask

What is a struct in the C language?

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, float, char, etc.).

Why do we use struct in C?

We use structures to overcome the drawback of arrays. We already know that arrays in C are bound to store variables that are of similar data types. Creating a structure gives the programmer the provision to declare multiple variables of different data types treated as a single entity.

Why is struct used?

'Struct' keyword is used to create a structure. A structure can contain variables, methods, static constructor, parameterized constructor, operators, indexers, events, and property. A structure can not derive/inherit from any structure or class. A structure can implement any number of interfaces.

What is structure in C with example?

Syntax of struct For example, struct Person { char name[50]; int citNo; float salary; }; Here, a derived type struct Person is defined. Now, you can create variables of this type.


3 Answers

If your struct is inside internal class then you get this warrning

internal class WrapperClass
{
    public struct ShFileInfo
    {
        public int hIcon;
        public int iIcon;
        public int dwAttributes;
    }
}   

when you change internal access to public then warnings gone:

public class WrapperClass
{
    public struct ShFileInfo
    {
        public int hIcon;
        public int iIcon;
        public int dwAttributes;
    }
}
like image 55
Piotr Pasieka Avatar answered Oct 28 '22 23:10

Piotr Pasieka


I had been receiving the warning when I copy pasted author's code. Warning received

You can define a constructor for the Struct like this

public struct ShFileInfo
    {
        public int hIcon;
        public int iIcon;
        public int dwAttributes;

        public ShFileInfo(int x,int y, int z)
        {
            hIcon = x;
            iIcon = y;
            dwAttributes = z;
        }
    }

You can also use a constructor with just one parameter and initialize all

  public ShFileInfo(int x)
        {
            hIcon = x;
            iIcon = 0;
            dwAttributes = 0;
        }
like image 24
Ajay Bhasy Avatar answered Oct 28 '22 23:10

Ajay Bhasy


Since you are using this structure for P/Invoke only, I'd simply disable the warning locally:

#pragma warning disable 0649
public struct ShFileInfo
{
    public int hIcon;
    public int iIcon;
    public int dwAttributes;
}
#pragma warning restore 0649

The compiler has no way of knowing that your structure is assigned in the unmanaged code, but since you know, there's little hurt in simply disabling the warning.

like image 43
Luaan Avatar answered Oct 28 '22 23:10

Luaan