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. How to declare an use a struct containing an array?

How to declare an use a struct containing an array?

Scheduled Pinned Locked Moved C#
questioncsharpdata-structurestutorial
11 Posts 8 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.
  • S scjsb

    I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    You declare an array with int[] and then you set it's size when you create it. mode = new int[10]; This can go in your contructor. If your array was of a class then they would all be null and would need to be newd individually, too. As they are int, they will all be fine, but I mention this for future reference.

    Christian Graus Driven to the arms of OSX by Vista.

    1 Reply Last reply
    0
    • S scjsb

      I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks

      M Offline
      M Offline
      moon_stick
      wrote on last edited by
      #3

      Why does it have to be a struct? Unless this is for Interop I don't see the value in storing a reference type within a value type - why not just declare it as a class?

      It definitely isn't definatley

      N 1 Reply Last reply
      0
      • S scjsb

        I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #4

        Personally, I'd make your struct a class, and use a generic list instead of an array. At that point you could do something like this:

        public class ChangeRec
        {
        private List<int> m_modes = new List<int>();

        public DateTime LastTimeStamp { get; set; }
        public int LastValue { get; set; }
        public List<int> Modes { get { return m\_modes; } }
        
        public ChangeRec() {}
        

        }

        There is now no need to initialize the list beyond what's done in the class. One other thing... I the variable "Last" refers to the last value added to the list. If that's the case, you can do this (uif you're using the code I posted above):

        ChangeRec changeRec = new ChangeRec();

        changeRec.Modes.Add(1);
        changeRec.Modes.Add(2);
        changeRec.Modes.Add(3);
        changeRec.Modes.Add(4);

        int lastValue = (int)(changeRec.Modes.Last());

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        modified on Monday, January 5, 2009 11:42 AM

        1 Reply Last reply
        0
        • S scjsb

          I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks

          R Offline
          R Offline
          Russell Jones
          wrote on last edited by
          #5

          i think the fixed keyword might help you unsafe public struct ChangeRec { public DateTime LastTimeStamp; public int last ; public fixed int mode[10] ; // I want an integer array of values } } The big problem here is that you'll have to allow unsafe code within your project so it's probably not the best plan unless you're planning to use interop. i'd be tempted to implement the thing as a class and create getMode(int i) and setMode(int i) methods. Russ

          1 Reply Last reply
          0
          • M moon_stick

            Why does it have to be a struct? Unless this is for Interop I don't see the value in storing a reference type within a value type - why not just declare it as a class?

            It definitely isn't definatley

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #6

            moon_stick wrote:

            storing a reference type within a value type

            What reference type?


            only two letters away from being an asset

            M 1 Reply Last reply
            0
            • N Not Active

              moon_stick wrote:

              storing a reference type within a value type

              What reference type?


              only two letters away from being an asset

              M Offline
              M Offline
              moon_stick
              wrote on last edited by
              #7

              An array is a reference type, no? Of type System.Array??

              It definitely isn't definatley

              N 1 Reply Last reply
              0
              • M moon_stick

                An array is a reference type, no? Of type System.Array??

                It definitely isn't definatley

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #8

                I guess I looked at the data type and ignored the array


                only two letters away from being an asset

                1 Reply Last reply
                0
                • S scjsb

                  I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks

                  D Offline
                  D Offline
                  Dragonfly_Lee
                  wrote on last edited by
                  #9

                  The reason is that C# does not allow us to have instance field initializer in struct. there is a workaround if you do not want to use the unsafe code, such as: struct MyStruct { public int last; public int[] mode; public int[] Mode { get { if (this.mode == null) { this.mode = new int[10]; } return this.mode; } } } The code above can be compiled successfully. Tan Li I Love KongFu~

                  1 Reply Last reply
                  0
                  • S scjsb

                    I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks

                    J Offline
                    J Offline
                    Jon Rista
                    wrote on last edited by
                    #10

                    Like everyone else, I would question why this is not a class. That doesn't answer your question, however. If you do indeed need a struct...which indicates you need it to be allocated on the stack, I would do something like the following (this might not be exact...havn't used stackalloc in a long time):

                    public struct ChangeRec
                    {
                    public ChangeRec(byte modeSize)
                    {
                    mode = stackalloc int[modeSize];
                    }

                    public DateTime LastTimeStamp;
                    public int Last;
                    public int\[\] mode;
                    

                    }

                    S 1 Reply Last reply
                    0
                    • J Jon Rista

                      Like everyone else, I would question why this is not a class. That doesn't answer your question, however. If you do indeed need a struct...which indicates you need it to be allocated on the stack, I would do something like the following (this might not be exact...havn't used stackalloc in a long time):

                      public struct ChangeRec
                      {
                      public ChangeRec(byte modeSize)
                      {
                      mode = stackalloc int[modeSize];
                      }

                      public DateTime LastTimeStamp;
                      public int Last;
                      public int\[\] mode;
                      

                      }

                      S Offline
                      S Offline
                      scjsb
                      wrote on last edited by
                      #11

                      Thanks for all your inputs, I've got it working as a class. I still don't know how to make this an array using this structure. What I need is an array of 200 records with each record having the structure as originally shown and guidance on how to actually use it.

                      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