Forum Topic: simple C# help?

(194 views • 4 replies)

This topic is 1 page long.

<< < > >>
None

TitusRevised

Reply To Post Reply & Quote

Posted at: 8/15/06 05:46 PM

TitusRevised LIGHT LEVEL 05

Sign-Up: 02/13/04

Posts: 391

Hello I'm learning C# and I'm having problems with my variables and methods and crap

I have a custom method called oppPress(); with a int variable in it.
Then i have a another method called numPress(); and I want to get the value of the variable in the oppPress method but i get this error "Error 3 The name 'oppVal' does not exist in the current context " and oppVal is the variable i'm trying to get.

I also get an error "The variable 'variable' is assigned but its value is never used
" but I am using the value in another method!

Help is appreciated.


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/15/06 05:50 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

if the variable is created in tat function, normally it can only be used in that function.

you will need to make the variable out side of the functions. it a global local thing you see, get used to it.


None

TitusRevised

Reply To Post Reply & Quote

Posted at: 8/15/06 06:16 PM

TitusRevised LIGHT LEVEL 05

Sign-Up: 02/13/04

Posts: 391

At 8/15/06 05:50 PM, thecoshman wrote: if the variable is created in tat function, normally it can only be used in that function.

you will need to make the variable out side of the functions. it a global local thing you see, get used to it.

What about making the variables global???


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/15/06 06:34 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

At 8/15/06 06:16 PM, TitusRevised wrote: What about making the variables global???

thats what i mean, if you declare the variable befor you make the functions you can use it any ware in your program, it is a global varaible. i am not sure about c#, but i dont think you can say you want a variable to be global.

there should be a like a int main() section, well in the first lines of that, declare the variables you want to use. you dont have to delcare all vaiables there, but if you declare them at the start of functions, means you can easerly see what variables you have to use.

go it yet? or do i need to go on? (im not trying to sound rude there btw)


None

0x41

Reply To Post Reply & Quote

Posted at: 8/15/06 07:11 PM

0x41 EVIL LEVEL 10

Sign-Up: 12/30/04

Posts: 551

C# doesn't support global variables. You need to set it as a class variable. I guess something like this is what you want?

class Blah
{
public void oppPress()
{
iVariable = 4;
}
public void numPress()
{
Console.WriteLine("{0}", iVariable);
}
static void Main(string[] args)
{
Blah b = new Blah();
b.oppPress();
b.numPress();
}
private int iVariable = 0;
}


All times are Eastern Standard Time (GMT -5) | Current Time: 09:13 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!