Literals into string [] - syntax problem

  • Thread starter Thread starter ryanm
  • Start date Start date
R

ryanm

I'm having a problem here, and it probably means I need to find a good
book on C#, but maybe someone can point it out quickly. I want to populate a
string array with a bunch of strings. So I'm doing this:

class foo{
public String[] StringList;
public foo(){
SetStrings();
}
private void SetStrings(){
StringList[0] = "Literal String 1";
}
}

Now, eventually I will be getting the strings from a remote machine
rather than just sticking a literal in there, but it's stopping me at this.
How do I put a string literal into a string array? There is no constructor
for a string[] type, so I'm not sure how I'm supposed to instantiate the
object, or how I can push values into it. Any help or pointers to docs would
be appreciated. The VS.NET docs are more confusing than helpful because of
all the VB, C++, JScript, etc docs that go with it, so searches take an hour
to sort through all the stuff that's for a different language.

thanks,
ryanm
 
You have to create the array with new

string[] str = new string[10]

Arrays in .net inherit from System.Array, so its "new" all the way

If you don't no the size of the array, use an ArrayList instead

ArrayList str = new ArrayList()

// add an ite
str.Add("Hello STR")

// extrac
string str2 = (string)str[1];
 
ryanm said:
I'm having a problem here, and it probably means I need to find a good
book on C#, but maybe someone can point it out quickly. I want to populate a
string array with a bunch of strings. So I'm doing this:
Never mind, I got it. I was declaring it with String[] instead of
string[]. Nothing better than a case problem to make you feel stupid.

ryanm
 
ryanm said:
I'm having a problem here, and it probably means I need to find a good
book on C#, but maybe someone can point it out quickly. I want to populate a
string array with a bunch of strings. So I'm doing this:

class foo{
public String[] StringList;
public foo(){
SetStrings();
}
private void SetStrings(){
StringList[0] = "Literal String 1";
}
}

Now, eventually I will be getting the strings from a remote machine
rather than just sticking a literal in there, but it's stopping me at this.
How do I put a string literal into a string array? There is no constructor
for a string[] type, so I'm not sure how I'm supposed to instantiate the
object, or how I can push values into it. Any help or pointers to docs would
be appreciated. The VS.NET docs are more confusing than helpful because of
all the VB, C++, JScript, etc docs that go with it, so searches take an hour
to sort through all the stuff that's for a different language.

You do: StringList = new string[numberOfElements];

or, to just put a set of values in, you can do:

StringList = new string[] {"Literal 1", "Literal2"};
 
ryanm said:
Never mind, I got it. I was declaring it with String[] instead of
string[]. Nothing better than a case problem to make you feel stupid.

That wouldn't make any difference at all, so long as you've got

using System;

at the top of your code.
 
Just as a side note, if you don't know the size of your string array when
you start, you can use:

System.Collections.Specialized.StringCollection

and then use the Add() method to add strings one at a time.

Also, you can apply the language filter to the VS.Net Docs.

Chris A.R.
 
Back
Top