declaring a string[] question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devicemangler
    New Member
    • Sep 2007
    • 5

    declaring a string[] question

    I have been trying to declare a string[] with a dynamic amount of elements (in C#). but everything i try gives me an error of some sort.

    i have tried

    Code:
    string[] myString = new string[];
    wont compile and gives an error of "Array creation must have array size or array initializer".
    Code:
    string[] myString = null;
    will compile but will give an "Object reference not set to an instance of an object" error.
    Code:
    string[] myString;
    gives a compile error of "Use of unassigned local variable "myString""
    none of these seem to work. help please.
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    You have to declare the size of the array.
    [CODE=csharp]string[] myString = new string[5];[/CODE]

    you can ccreate it dynamically too i guess
    Code:
    string[] myString = new string[(i*4)];
    cheers

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      There are many choices, if you truely CANNOT be certain of the size you will need, even at runtime, consider using something from the System.Collecti ons namespace. (I use ArrayList, but there are plenty of others).
      Once you have a populated ArrayList (or collection of some sort) they have a function that can be used to turn it into a pure array (such as string[])

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by devicemangler
        I have been trying to declare a string[] with a dynamic amount of elements (in C#). but everything i try gives me an error of some sort.

        i have tried

        Code:
        string[] myString = new string[];
        wont compile and gives an error of "Array creation must have array size or array initializer".
        Code:
        string[] myString = null;
        will compile but will give an "Object reference not set to an instance of an object" error.
        Code:
        string[] myString;
        gives a compile error of "Use of unassigned local variable "myString""
        none of these seem to work. help please.
        If your C# textbook has no section for declaring and initializing arrays, then shame on its author and shame on the guy who recommended that book to you.

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #5
          Originally posted by r035198x
          If your C# textbook has no section for declaring and initializing arrays, then shame on its author and shame on the guy who recommended that book to you.
          Sometimes its an advantage to have learnt C :P
          You learn to debug that way.

          Comment

          • devicemangler
            New Member
            • Sep 2007
            • 5

            #6
            I found a workaround for it, the project i am working on requires me to load data from a database into an array, so i first made a query to the table to find out how many records it held, then used that number in the declaration of the array.

            it works for now. also the book i have been using is "Sams teach yourself c# in 21 days" though i've been using the net a lot more than the book (the book only barely scratches the surface of winforms).

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by devicemangler
              I found a workaround for it, the project i am working on requires me to load data from a database into an array, so i first made a query to the table to find out how many records it held, then used that number in the declaration of the array.

              it works for now. also the book i have been using is "Sams teach yourself c# in 21 days" though i've been using the net a lot more than the book (the book only barely scratches the surface of winforms).
              Don't connect to the database twice to get one set of data. Just use an ArrayList instead. If you really need to use an array you can create the array from the ArrayList. If Sam didn't talk about dynamic arrays in his book then shame on him.

              Comment

              Working...