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. String Array in a struct

String Array in a struct

Scheduled Pinned Locked Moved C#
questiondata-structures
8 Posts 4 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.
  • J Offline
    J Offline
    JF2015
    wrote on last edited by
    #1

    Hi, i want an string-array with a fixed size within a struct. How can I achieve this? The normal way to initialize the array doesn't work. Also using fixed is no solution because it does not support the type string (a string has no fixed size).

    public struct Order
    {
    public string owner;
    public string prognr;
    public string number = new string[20];
    }

    Thanks in advance

    S V 2 Replies Last reply
    0
    • J JF2015

      Hi, i want an string-array with a fixed size within a struct. How can I achieve this? The normal way to initialize the array doesn't work. Also using fixed is no solution because it does not support the type string (a string has no fixed size).

      public struct Order
      {
      public string owner;
      public string prognr;
      public string number = new string[20];
      }

      Thanks in advance

      S Offline
      S Offline
      sam
      wrote on last edited by
      #2

      you can use class in the same way public class Order { public string owner; public string prognr; public string []number = new string[20]; }

      1 Reply Last reply
      0
      • J JF2015

        Hi, i want an string-array with a fixed size within a struct. How can I achieve this? The normal way to initialize the array doesn't work. Also using fixed is no solution because it does not support the type string (a string has no fixed size).

        public struct Order
        {
        public string owner;
        public string prognr;
        public string number = new string[20];
        }

        Thanks in advance

        V Offline
        V Offline
        Vikram A Punathambekar
        wrote on last edited by
        #3

        JF2015 wrote:

        public string number = new string[20];

        That should be

        public string[] number = new string[20];

        Cheers, Vıkram.


        Déjà moo - The feeling that you've seen this bull before. Join the CP group at NationStates. Password: byalmightybob

        J 1 Reply Last reply
        0
        • V Vikram A Punathambekar

          JF2015 wrote:

          public string number = new string[20];

          That should be

          public string[] number = new string[20];

          Cheers, Vıkram.


          Déjà moo - The feeling that you've seen this bull before. Join the CP group at NationStates. Password: byalmightybob

          J Offline
          J Offline
          JF2015
          wrote on last edited by
          #4

          Hi Vikram, if I try this I get an error saying: Compiler Error CS0573 Error Message 'field declaration' : cannot have instance field initializers in structs So, obviously I can't initialize this array within the struct, but initializing the array out of the struct as:

          Order or = new Order();
          or.Number = new or.Number[20];

          is also not working.

          C 1 Reply Last reply
          0
          • J JF2015

            Hi Vikram, if I try this I get an error saying: Compiler Error CS0573 Error Message 'field declaration' : cannot have instance field initializers in structs So, obviously I can't initialize this array within the struct, but initializing the array out of the struct as:

            Order or = new Order();
            or.Number = new or.Number[20];

            is also not working.

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

            Create a default constructor that initialises the field.

            JF2015 wrote:

            or.Number = new or.Number[20];

            That's never going to work, what do you expect it to do ? or.Number = new string[20]; assuming it's an array of strings, is what you need.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            J 1 Reply Last reply
            0
            • C Christian Graus

              Create a default constructor that initialises the field.

              JF2015 wrote:

              or.Number = new or.Number[20];

              That's never going to work, what do you expect it to do ? or.Number = new string[20]; assuming it's an array of strings, is what you need.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              J Offline
              J Offline
              JF2015
              wrote on last edited by
              #6

              Ok, my fault, i originally thought that i had to initialize the array within the struct, but didn't know i had to do it like you wrote. Thanks alot

              C V 2 Replies Last reply
              0
              • J JF2015

                Ok, my fault, i originally thought that i had to initialize the array within the struct, but didn't know i had to do it like you wrote. Thanks alot

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

                Glad to help :-)

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                1 Reply Last reply
                0
                • J JF2015

                  Ok, my fault, i originally thought that i had to initialize the array within the struct, but didn't know i had to do it like you wrote. Thanks alot

                  V Offline
                  V Offline
                  Vikram A Punathambekar
                  wrote on last edited by
                  #8

                  I'm sorry, I wasn't paying attention to the fact that you wanted it inside a type.

                  Cheers, Vıkram.


                  Déjà moo - The feeling that you've seen this bull before. Join the CP group at NationStates. Password: byalmightybob

                  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