470,573 Members | 1,668 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,573 developers. It's quick & easy.

Q: String array

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
Sep 19 '06 #1
10 2123
"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote in
message news:eD**************@TK2MSFTNGP04.phx.gbl...
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.
Sep 19 '06 #2
Hi Martin,

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

For regular arrays size (length) is locked upon creation and cannot be
changed.
On Tue, 19 Sep 2006 15:39:05 +0200, Visual Systems AB (Martin Arvidsson)
<ma**************@vsab.netwrote:
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



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 19 '06 #3
"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote
in message news:eD**************@TK2MSFTNGP04.phx.gbl...
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

Sep 19 '06 #4
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<stringmyList = 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

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote in
message news:eD**************@TK2MSFTNGP04.phx.gbl...
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

Sep 19 '06 #5
Hi,

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

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netschrieb
im Newsbeitrag news:eD**************@TK2MSFTNGP04.phx.gbl...
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

Sep 19 '06 #6
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)" <ma**************@vsab.netskrev i
meddelandet news:eD**************@TK2MSFTNGP04.phx.gbl...
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

Sep 19 '06 #7
Hi, loo inside,

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netschrieb
im Newsbeitrag news:%2****************@TK2MSFTNGP03.phx.gbl...
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)" <ma**************@vsab.netskrev i
meddelandet news:eD**************@TK2MSFTNGP04.phx.gbl...
>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


Sep 19 '06 #8
Split creates a new instance of a string array of the appropriate size for
the parsed data.

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
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)" <ma**************@vsab.netskrev i
meddelandet news:eD**************@TK2MSFTNGP04.phx.gbl...
>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


Sep 19 '06 #9

"ChrisM" <ch**************@suedeyahoo.comwrote in message
news:uv**************@TK2MSFTNGP06.phx.gbl...
"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote
in message news:eD**************@TK2MSFTNGP04.phx.gbl...
>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.
Sep 19 '06 #10
"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote
>
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<stringclass. This code would look like:

List<stringmyStringList = 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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Sep 20 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Don Starr | last post: by
7 posts views Thread by al | last post: by
4 posts views Thread by songkv | last post: by
22 posts views Thread by spike | last post: by
4 posts views Thread by Simon Schaap | last post: by
8 posts views Thread by Jeff Johnson | last post: by
17 posts views Thread by Chad Myers | last post: by
11 posts views Thread by Zordiac | last post: by
14 posts views Thread by Shhnwz.a | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.