Arrays in C# structs?
-
Hi, I am trying to convert c++ code into c# code, and I am having trouble with arrays in structs. The c++ struct looks like the following: typedef struct { int nAs; int As[8]; int nBs; int Bs[12]; int nCs; int Cs[30]; } AN_ENTRY; After quite a bit search, I figured out the definition can be changed to this: [StructLayout(LayoutKind.Sequential, Pack = 4)] public struct AnEntry { public int nAs; [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)] public int[] As; public int nBs; [MarshalAs(UnmanagedType.ByValArray, SizeConst=12)] public int[] Bs; public int nCs; [MarshalAs(UnmanagedType.ByValArray, SizeConst=30)] public int[] Cs; } But how would I go around and initialize such a struct, I mean they are 53 integers that need to be passed in to initialize this? Can I do the following? AnEntry anEntry = new AnEntry(1, 1, 1, 1, ....., 1, 1); // 53 1's Do I have a define a constructor in the struct? If yes, the argument list of the constructor would be REALLY long. What if I need to create an array of AnEntry? Any suggestion or comment is highly appreciated. Thanks a lot.
-
Hi, I am trying to convert c++ code into c# code, and I am having trouble with arrays in structs. The c++ struct looks like the following: typedef struct { int nAs; int As[8]; int nBs; int Bs[12]; int nCs; int Cs[30]; } AN_ENTRY; After quite a bit search, I figured out the definition can be changed to this: [StructLayout(LayoutKind.Sequential, Pack = 4)] public struct AnEntry { public int nAs; [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)] public int[] As; public int nBs; [MarshalAs(UnmanagedType.ByValArray, SizeConst=12)] public int[] Bs; public int nCs; [MarshalAs(UnmanagedType.ByValArray, SizeConst=30)] public int[] Cs; } But how would I go around and initialize such a struct, I mean they are 53 integers that need to be passed in to initialize this? Can I do the following? AnEntry anEntry = new AnEntry(1, 1, 1, 1, ....., 1, 1); // 53 1's Do I have a define a constructor in the struct? If yes, the argument list of the constructor would be REALLY long. What if I need to create an array of AnEntry? Any suggestion or comment is highly appreciated. Thanks a lot.
Usually when your passing a new array of something into a constructor / parameter, you would do something like this: AnEntry anEntry = new AnEntry(new int[53]{1,1,1,1,1,1,1,1...}) ; make sense?
-
Usually when your passing a new array of something into a constructor / parameter, you would do something like this: AnEntry anEntry = new AnEntry(new int[53]{1,1,1,1,1,1,1,1...}) ; make sense?
-
But do I need to create a constructor inside the struct? If yes, I will have to list all those 53 parameters? Thanks.
Where do you take data for the struct instances? If data are taken from a database then you can create constructor with one parameter (record identifier) and load data within this constructor. Or you can add Load(int id) method to your struct. If data are random then you can add FillRandom() method. And so on.