[AGDev-newbies] Who you gonna call?

Thomas Ward tward1978 at earthlink.net
Sun May 7 19:30:54 BST 2006


Hi Mike.
Remove the int from

 int myVar = 84;
 and that will fix your problem.

Here is a lesson in scope. If something is declared globally that means 
at the top of your class all the functions in that class can access it, 
and it only needs to be declared once with the float, int, etc.
If it is a local variable, my var is a local variable, it can be 
declared as int somewhere else in the program, but not in the same 
function or scope which is in your program function main.
Some examples are in order to ilistrate this.

class My Class
{

// I am a global variable.
int g_iVariable = 0;

public void NewVariable()
{

// declare a local variable.
int iVariable = 0;

// Change the global variable here.
g_iVariable = 23;

// Change my local variable here.
// This function knows the local variable exists so
// need for int.
iVariable = 99;
}
}


Mike Maslo wrote:
> List:
>
> Help already. I have created this little program and when I try to run it I
> get a error saying that the local variable is already in use in this scope.
>
> I am trying to make the variable myVar have one number and after it assigns
> and prints it out on the screen, I want to re assign it to a different
> number.
>
> What am I doing wrong??
>
> Please help.
>
> namespace Frustrated
> {
>     class Trusted
>     {
>         static void Main()
>         {
>             int myVar = 16;
>             System.Console.WriteLine("initialized,  myVar: {0} ", 
>                 myVar);
>             int myVar = 84;
>             System.Console.WriteLine("next variable is: {0} " ,
>             blew);
>         }
>     }
> }
>
>   


More information about the AGDev-newbies mailing list