newbie Q: creating a global variable array of strings?

ThienZ

Well-known member
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 dont work....

how can i write this right?

thx
 
Sorry, Im not 100% familiar with c#. Im trying to help, so no one get mad if Im wrong aboot anything. Since String[] is a class (System.Array), I dont think that const is what you are looking for. With a class, if you dont 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:
Code:
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:

[VB]
public class myClass2 {
private char [] alphabeths; //I believe that readonly would work here too

public void myClass() {
alphabeths = "abcdefghijklmnopqrstuvwxyz".ToCharArray;
}
}
[/VB]
 
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:
Code:
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