Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Arrays in C# structs?

Arrays in C# structs?

Scheduled Pinned Locked Moved C#
csharpc++data-structuresquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kuang Cao
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • K Kuang Cao

      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.

      P Offline
      P Offline
      Paul Brower
      wrote on last edited by
      #2

      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?

      K 1 Reply Last reply
      0
      • P Paul Brower

        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?

        K Offline
        K Offline
        Kuang Cao
        wrote on last edited by
        #3

        But do I need to create a constructor inside the struct? If yes, I will have to list all those 53 parameters? Thanks.

        A 1 Reply Last reply
        0
        • K Kuang Cao

          But do I need to create a constructor inside the struct? If yes, I will have to list all those 53 parameters? Thanks.

          A Offline
          A Offline
          Andrew Lygin
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups