ThienZ Posted March 16, 2005 Posted March 16, 2005 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 Quote
Leaders snarfblam Posted March 16, 2005 Leaders Posted March 16, 2005 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; } } Quote [sIGPIC]e[/sIGPIC]
forgottensoul Posted March 16, 2005 Posted March 16, 2005 Have you tried: private const string alphabeths[] = {"a", "b", "c"}; Quote
Leaders snarfblam Posted March 17, 2005 Leaders Posted March 17, 2005 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) Quote [sIGPIC]e[/sIGPIC]
ThienZ Posted March 17, 2005 Author Posted March 17, 2005 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 :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.