newbie Q: creating a global variable array of strings?

ThienZ

Freshman
Joined
Feb 10, 2005
Messages
45
Location
Germany
i tried this :
C#:
public class myClass {
  private const string [] alphabeths = {"a", "b", "c"};
and this :
C#:
public class myClass {
  private string [] alphabeths;
  public myClass() {
    alphabeths = {"a", "b", "c"};
and both don't work....

how can i write this right?

thx
 
Sorry, I'm not 100% familiar with c#. I'm trying to help, so no one get mad if I'm wrong aboot anything. Since String[] is a class (System.Array), I don't think that const is what you are looking for. With a class, if you don't want it to be modified, I think you would want to use readonly, which specifies that a variable can be modified only by initializers and the constuctor. The elements will still be modifiable, however, just not the reference to the array.

In the second example I believe that you need to instantiate a new array before you initialize it.

Here:
Visual Basic:
public class myClass1 {
	private readonly string [] alphabeths = {"a", "b", "c"};
}

public class myClass2 {
  private string [] alphabeths;

  public void myClass() {
  	alphabeths = new string[] {"a", "b", "c"}; //'This could have also been readonly
  }
}

Also, depending on your needs (specifically on whether you are dealing with single charatcer strings), a char array might work. Try this:

Visual Basic:
public class myClass2 {
  private char [] alphabeths; //'I believe that readonly would work here too

  public void myClass() {
  	alphabeths = "abcdefghijklmnopqrstuvwxyz".ToCharArray;
  }
}
 
I just tried that in #develop, and it came up as a build error telling me that the brackets need to come before the identifier (i.e. on the type specifier, "strings", i assume)
 
marble_eater said:
Visual Basic:
public class myClass2 {
  private string [] alphabeths;

  public void myClass() {
  	alphabeths = new string[] {"a", "b", "c"}; //'This could have also been readonly
  }
}

thx marble_eater, this works :)
 
Back
Top