anon271334
anon271334

Reputation:

Adding to an Array

I have an array:

String[] ay = {
     "blah",
     "blah number 2"
     "etc" };

... But now I want to add to this array at a later time, but I see no option to do so. How can this be done? I keep getting a message saying that the String cannot be converted to String[].

Thank you

Upvotes: 3

Views: 415

Answers (8)

kemiller2002
kemiller2002

Reputation: 115420

Here's an extension method to add the to arrays together and create a new string array

public static class StringArrayExtension
{
    public static string[] GetStringArray (this string[] currentArray, string[] arrayToAdd)
    {
      List<String> list = new List<String>(currentArray);

      list.AddRange(arrayToAdd);   


      return list.ToArray();
    }
}

Upvotes: 0

MUG4N
MUG4N

Reputation: 19717

You can do this but I don't recommend it:

// Reallocates an array with a new size, and copies the contents
// of the old array to the new array.
// Arguments:
//   oldArray  the old array, to be reallocated.
//   newSize   the new array size.
// Returns     A new array with the same contents.
public static System.Array ResizeArray (System.Array oldArray, int newSize) {
   int oldSize = oldArray.Length;
   System.Type elementType = oldArray.GetType().GetElementType();
   System.Array newArray = System.Array.CreateInstance(elementType,newSize);
   int preserveLength = System.Math.Min(oldSize,newSize);
   if (preserveLength > 0)
      System.Array.Copy (oldArray,newArray,preserveLength);
   return newArray; 

}

Upvotes: 0

Luke
Luke

Reputation: 3433

If you do need indexing have a look at Dictionary data type also in the System.Collection

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

so you could do something like

        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        dictionary.Add(1, "afljsd");

Upvotes: 0

Bogi
Bogi

Reputation: 2588

If you don't need indexing a specific array element (usage of brackets), but you want to be able to efficiently add or remove elements, you could use LinkedList.

Upvotes: 0

Skintkingle
Skintkingle

Reputation: 1579

As everyone's already said, use List in the System.Collections.Generic namespace. You could also use a Hashtable which will allow you to give each string a meaning, or "key" which gives you an easy way to pull out a certain string with a keyword. (as for keeping messages stored in memory space for whatever purpose.) You could also Create a new array each time you add a value, make the new array 1 bigger than the old one, copy all the data from the first array into the 2nd array, and then add your new value in the last slot (Length - 1) Then replace the old array with your new one. It's the most manual way of doing it. But List and Hashtable work perfectly well too.

Upvotes: 0

Arjan Einbu
Arjan Einbu

Reputation: 13672

Arrays are of fixed size, so after it has been created, you can't change the size of it (without creating a new array object)

Use the List<string> instead of the array.

Upvotes: 3

Petar Minchev
Petar Minchev

Reputation: 47363

Arrays can't change their size after they are declared. Use collections instead. For example: List.

Upvotes: 2

tim_yates
tim_yates

Reputation: 171054

Use a List rather than an array:

List<string> list = new List<string>();
list.Add( "blah" ) ;

Then, later, if you really do need it as an array:

string[] ay = list.ToArray();

Upvotes: 6

Related Questions