none
Populating an Array

    Question

  • Hi,

     

    Can someone tell me how i can insert a string value into an array of type string.

    This is the code i have.

     

    String[] listPoints;

    String val1;

    for(int i = 0; i < .....;i++)

    {

       val1 = "Hello";

        listPoints[  i  ] = val1;   //had to put spacing in the [     i       ]  as they shows up as a light bulb

    }

     

    Im getting an error with using this kind of insert into the array?

     

    Anyone tell me why?

     

    Thanks.

    Thursday, March 29, 2007 4:21 PM

Answers

All replies

  • Hi,

     

    try initializing your listPoints array:

     

    Code Snippet

    int myCount = 50;

    string[] listPoints = new string[myCount];

    string val1;

    for (int i = 0; i < myCount; i++)

    {

        val1 = "Hello";

        listPoints[i] = val1;

    }

      

    Andrej

    Thursday, March 29, 2007 4:35 PM
  • Hi,

     

    Thanks for quick response.

     

    Is it possible to leave out setting the size of the array when creating it?

     

    Is there much difference in using a 2d array.

     

    I think i create it like this?

     

     

    Code Snippet
    String[,] listPoints = new String[,];

     

    Thursday, March 29, 2007 4:42 PM
  • Hi,

    no,

    u have to speicify the dimensions when u go for rectangular arrays.

    when u go for jagged arrays, for a two dimensional one,u have to specify the dimension1.

     

    Thanx,

    Ch.T.Gopi Kumar.

     

    Thursday, March 29, 2007 5:02 PM
  • So i always have to specify the size of the array?
    Thursday, March 29, 2007 5:04 PM
  • I created an array list to solve the problem

     

    Thanks for the help.

    Thursday, March 29, 2007 6:25 PM
  • Depending on your needs, you may also use Generic List<string>, which will allow you to add, change or remove items without having to fix it size at declaration. When needed, you can use its ToArray() method to return plain string[] array, containing string items.

     

    Andrej

    Thursday, March 29, 2007 6:34 PM
  • Hi,

    when u instantiate the array , u must know the dimensions.

     

    Thanx,

    Ch.T.Gopi Kumar.

    Friday, March 30, 2007 7:04 AM