This is my code:
public Global()
{
arreglo =
new int[3000];}
public static int [] Arreglo{
get { return arreglo; } set { arreglo = value; }}
public static int SetValue(int pos, int value){
arreglo[pos] = value;
}
public static int GetValue(int pos){
return arreglo[pos];}
}
Can anyone help me with my problem? Thank you very much.
Kelvin LokePosted Jun 11, 2007, 4:00 PM
public static class Global
{
static int[] arreglo = new int[3000]; public static int [] Arreglo{
get { return arreglo; } set { arreglo = value; }}
public static void SetValue(int pos, int value){
arreglo[pos] = value;
}
public static int GetValue(int pos){
return arreglo[pos];}
}
Thank you again for your help :). Take care
AlanPosted Jun 11, 2007, 3:44 PM
static int[] arreglo = new int[3000];
Also the return type of the SetValue() method should be void rather than int.
To answer your question, Brandon, the general advice is to use a get/set property, plus a private/protected field, rather than a public or internal field, if:
1. The field can only be set to certain values and not any value which its type allows.
2. Some other subsidiary code needs to be executed when the value is set or accessed.
3. Even though neither 1. or 2. applies currently, you think they might conceivably do so in the future.
The reason for 3. is that if you change a public field to a public property at a later date, it changes the interface of the containing class.
A lot of programmers never use public read/write fields, though I occasionally use them myself if the above criteria are satisfied.
Posted Jun 11, 2007, 3:38 PM
Sure thing. C# took global variables out due to the fact that they were unsafe because they could be accessed by code anywhere in the program. the closest thing Ive found is declaring the variable as an internal variable and sticking it in the Main() class so it can be used by all code within the main class. Good luck!
Brandon
Kelvin LokePosted Jun 11, 2007, 3:32 PM
Posted Jun 11, 2007, 3:22 PM
Kelvin LokePosted Jun 11, 2007, 3:09 PM
Invalid token ';' in class, struct, or interface member declaration
Invalid token '=' in class, struct, or interface member declaration
arreglo =
new int[3000];I also get this error: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) , but this error is easy to fix.. so.. any other idea? thank you!
Posted Jun 11, 2007, 2:49 PM
I hate not having a compiler here at work because I cant test these things out :(
Brandon
EDIT:
Also Alan, I have a question. When would it be applicable to use get and set? Ive never used them in anything Ive done and Ive built a few pretty hefty applications... I tend to only learn something new if I have to use it. Like writing, reading, and searching XML files.
AlanPosted Jun 11, 2007, 2:44 PM
public static class Global
{
arreglo = new int[3000];
public static int [] Arreglo
{
get { return arreglo; }
set { arreglo = value; }
}
public static int SetValue(int pos, int value)
{
arreglo[pos] = value;
}
public static int GetValue(int pos)
{
return arreglo[pos];
}
}
Kelvin LokePosted Jun 11, 2007, 2:31 PM
Invalid token 'return' in class, struct, or interface member declaration
Any idea? thank you for your time :).
Posted Jun 11, 2007, 1:06 PM
"Not all code paths return a value" means that somewhere in your code there is an accessible sequence that wont return a value and would be a "dead end" sequence. Try putting a return 0; statement between your last two block brackets and that should take care of it.
public Global()
{
arreglo = new int[3000];
}
public static int [] Arreglo
{
get { return arreglo; }
set { arreglo = value; }
}
public static int SetValue(int pos, int value)
{
arreglo[pos] = value;
}
public static int GetValue(int pos)
{
return arreglo[pos];
}
//Here, that way in case it skips int [] Arreglo, itll still return some sort of a value
return 0;
}
PS. Im at work right now so I cant test it out, but at least you have an idea of whats wrong, good luck!
Brandon