Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiler error: "cannot have instance field initializers in structs"

Tags:

I need advice on structures.

I have 2 sections of code. The first section is as below:

namespace Project.GlobalVariables {     class IOCard     {         struct InputCard         {             public string CardNo;             public int BaseAddress;             public int LowerAddress;             public int UpperAddress;             public int[] WriteBitNo = new int[16];             public int[] ReadBitNo = new int[16];         }          static InputCard[] InputCards = new InputCard[5];          public static string ACardNo = InputCards[1].CardNo;         public static string BCardNo = InputCards[2].CardNo;      } } 

The second portion is as below:

private void Form1_Load(object sender, EventArgs e)     {         IOCard.ACardNo = "Card A";         IOCard.BCardNo = "Card B";          MessageBox.Show(IOCard.ACardNo);         MessageBox.Show(IOCard.BCardNo);     } 

My plan is to be able to assign and retrieve InputCards component by using IOCard as shown in Form1_Load.

However, when I compile the code, I get the following error.

Error 1 'Project.GlobalVariables.IOCard.InputCard.WriteBitNo': cannot have instance field initializers in structs E:\Programming\New platform\StandardPlatform\StandardPlatform\Project\GlobalVariables.cs 16 26 StandardPlatform

Can someone tell me how to solve the error? Please advise. Thanks.

Here are the classes that I have attempted to create and use, but failed.

using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace Project.GlobalVariables {     static class IOCard     {         public const int TotalInputCard = 10;         public const int TotalOutputCard = 10;          public  class InputCard         {             public string CardNo = "1";             public int BaseAddress;             public int LowerAddress;             public int UpperAddress;             public int[] WriteBitNo = new int[16];             public int[] ReadBitNo = new int[16];         }          public class OutputCard         {             public string CardNo;             public int BaseAddress;             public int LowerAddress;             public int UpperAddress;             public int[] WriteBitNo = new int[16];             public int[] ReadBitNo = new int[16];         }          public static InputCard[] InputCards = new InputCard[TotalInputCard];         public static OutputCard[] OutputCards = new OutputCard[TotalOutputCard];          public static int X100 = InputCards[0].WriteBitNo[0];         public static int Y100 = OutputCards[0].WriteBitNo[0];     } } 

I tried to use these in the Form_Load, like so:

private void Form1_Load(object sender, EventArgs e) {     IOCard.X100 = 1;     IOCard.Y100 = 1; }  

No matter how much I have tried to search on the net for answers, I have got nowhere.

Please advise. Thanks.

like image 440
JoJo Avatar asked Dec 10 '10 06:12

JoJo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

What's it's trying to say is that when you have InputCards = new InputCard[5]; it will allocate a block of memory 5 times the size of an InputCard structure and set all of its bytes to 0. There is no opportunity to execute the int[] WriteBitNo = new int[16]; and such assignments, so you cannot have them.

Your options are to either manually call an initializer for your structs or make it a class and manually initialize the InputCards array with 5 new instances of InputCard.

like image 31
Gabe Avatar answered Oct 13 '22 00:10

Gabe


In C#, a struct value is not a reference to an object in the way a value of a class type is. The value of a struct is the "union" of all the values of the instance fields of the struct.

Now, the default value of a struct type is the value where all those fields have their default values. Since the beginning of C#, the syntax:

new S()  // S is a value-type 

where S is a struct type, has been equivalent to the default value of that struct. There is no constructor call! This is the exact same value which can (nowadays) also be written

default(S)  // S is a value-type 

Now, things like

struct S {   int field = 42; // non-static field with initializer, disallowed!    // ... } 

are illegal (cannot have instance field initializers in structs). They could give the impression that the field of a new S() would be 42, but in fact the field of new S() must be the default value of int (which is zero, distinct from 42).

With this explanation, you also see why it is not possible to create a non-static, zero-parameter constructor for a struct type, in C#.

like image 173
Jeppe Stig Nielsen Avatar answered Oct 13 '22 01:10

Jeppe Stig Nielsen