Q: String array

  • Thread starter Thread starter Visual Systems AB \(Martin Arvidsson\)
  • Start date Start date
V

Visual Systems AB \(Martin Arvidsson\)

Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin
 
Visual Systems AB (Martin Arvidsson) said:
Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin

You're right, you can't fill it with more than one element, but as a string
array, the single element you CAN fill it with can be as many characters as
you want (within reason).
So:

string[] myStringArray = new string[0];
myStringArray[0]="A Nice Long string could quite happily go here...";
myStringArray[1]="Another string? UhOh, it is not going to like this...";

The second assignment would cause a runtime error as the array has only been
defined with a single element.

Maybe you are thinking of the fact that a string is kind of an array of
characters...?

Chris.
 
Hi Martin,

If you need to add and/or remove lines from an array, use an ArrayList or
a generic List<T> instead.

For regular arrays size (length) is locked upon creation and cannot be
changed.


Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin
 
Visual Systems AB (Martin Arvidsson) said:
Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Actually, you can't put anything in it...it has a size of 0
new string[1] would hold one string
new string[0] would hold zero strings

Suppose you need an array but you don't know how many elements it will
hold. For whatever reason you don't wish to use an ArrayList or some
other dynamic collection. a size zero array makes a better default than
a null reference. Once you know the data you can replace the empty array
with an array of size > 0 containing real data

then you could write something like this without a check for null
foreach (string str in myStringArray)
{
Blah();
}

This will work even if the size of the array is 0.

Hope this helps
Bill
 
Martin

For a start you've created an array with no space, although that may have
been your intention. In C# you set the initial size of the array by setting
how many elements you want and not the upper bound ordinal. To "expand" the
array you'll need to actually create a new instance of the array with more
elements and copy the contents across.

string[] myArray = new string[1];

myArray[0] = "x";

string[] tempArray = new string[2];

myArray.CopyTo( tempArray, 0 );

myArray = tempArray;

myArray[1] = "y";

Not my best code and not very efficient, but hopefully you get the idea.

Personally, if I don't know the size of an array I either use an ArrayList
..net 1.x or, in .Net 2 a generic like List<string>

List<string> myList = new List<string>();
myList.Add( "x" );
myList.Add( "y" );

//if you want it as a string[] array...

string[] myArray = myList.ToArray();

..Net provides a whole bunch of different types of collections for you to use
built for different tasks. See the System.Collections namespace
documentation.

HTH

Glenn
 
Hi,

you can't change the size of an array instance.
If you want a growning list you should use ArrayList or List<string>
 
Hmmm!

if i do like this...

string[] myStringArray = new string[0];
myStringArray = MyData.Split(new char[] ';');

will fill my array with x data from my data? How is that possible if the
string created can't contain any elements?

/Martin
 
Split creates a new instance of a string array of the appropriate size for
the parsed data.

Visual Systems AB (Martin Arvidsson) said:
Hmmm!

if i do like this...

string[] myStringArray = new string[0];
myStringArray = MyData.Split(new char[] ';');

will fill my array with x data from my data? How is that possible if the
string created can't contain any elements?

/Martin

Visual Systems AB (Martin Arvidsson) said:
Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin
 
Hi, loo inside,

Visual Systems AB (Martin Arvidsson) said:
Hmmm!

if i do like this...

string[] myStringArray = new string[0];
myStringArray = MyData.Split(new char[] ';');

will fill my array with x data from my data?

No it wan't. It creates a new Array and asigns it to the variable.
The array created in the first statement will be lost (and soon
collected by the GC.

test this:
string[] array1 = new string[0];
string[] array2 = array1;
array1 = MyData.Split(new char[] {';'});

array2 will still be empty.
How is that possible if the string created can't contain any elements?

/Martin

Visual Systems AB (Martin Arvidsson) said:
Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin
 
ChrisM said:
Visual Systems AB (Martin Arvidsson) said:
Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin

You're right, you can't fill it with more than one element, but as a
string array, the single element you CAN fill it with can be as many
characters as you want (within reason).
So:

string[] myStringArray = new string[0];
myStringArray[0]="A Nice Long string could quite happily go here...";
myStringArray[1]="Another string? UhOh, it is not going to like this...";

The second assignment would cause a runtime error as the array has only
been defined with a single element.

Maybe you are thinking of the fact that a string is kind of an array of
characters...?

Chris.

Oops that'll teach me to post without thinking. As has already been pointed
out, the declaration in the above code creates a zero length array.
Although it seems like your question has already been answered, to make what
I wrote correct, it should be:
string[] myStringArray = new string[1];

Chris.
 
Visual Systems AB (Martin Arvidsson) said:
Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

In .Net, it's actually pretty rare to need to create a string array. You're
much better off using a List<string> class. This code would look like:

List<string> myStringList = new List<String>();
myStringList.Add("One");
myStringList.Add("Two");
myStringList.Add("Three");

From here you can convert this to a string array (if that's really what you
need), or just use the various methods on the List class for accessing a
particular string.
 
Back
Top