Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i tried this :

public class myClass {
 private const string [] alphabeths = {"a", "b", "c"};

and this :

public class myClass {
 private string [] alphabeths;
 public myClass() {
   alphabeths = {"a", "b", "c"};

and both don't work....

 

how can i write this right?

 

thx

  • Leaders
Posted

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:

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:

 

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

 public void myClass() {
 	alphabeths = "abcdefghijklmnopqrstuvwxyz".ToCharArray;
 }
}

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted
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)
[sIGPIC]e[/sIGPIC]
Posted

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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...