Reputation: 3
I currently have a school project and I'm making a game. Here's my code:
public partial class room2 : Form
{
public room2()
{
InitializeComponent();
Random rand1 = new Random();
Int32 t =6 ;
Int32 Fight = rand1.Next(1, 11);
Int32 J = 10;
label4.Text = Convert.ToString(10);
if (Fight <= t)
{
label3.Text = Convert.ToString(J);
}
else
{
txtDialogBox.Text = ("No Fight in this room");
}
}
private void Attack_Click(object sender, EventArgs e)
{
Random rand2 = new Random();
Int32 J = 10;
Int32 Attack = rand2.Next(1, 4);
Int64 y = 1;
Int64 t = 10;
//opponents hp bar goes down by 1
J --;
label3.Text = Convert.ToString(J);
// chance for your hp bar to go down
if (Attack >= y)
{
label4.Text = Convert.ToString(t);
t--;
}
}
}
When I put the Ints at the top (like I was told to ) I get errors ("does not exist in current context") and I found that the only way for me to fix it is to put it in with the button.
Upvotes: 0
Views: 140
Reputation: 49
koko,
As you may know, room2 is the constructor of the form class you are in. (as you have it implementing form with the : operator)
you could declare the int's outside the constructor by setting those variables access modifiers.
try:
public partial class room2 : Form
{
private int32 J=10;
private Int64 y = 1;
private Int64 t = 10;
private Int32 J = 10;
private Int32 t = 6 ;
//This would mean J is a private variable that only members of this class can access
//This is because it is declared at the top of the class (like you said, as you were advised to do)
//If you declare this in the constructor, other class elements cannot access it.
//OTHER Variable code here then:
public room2()
{
InitializeComponent();
Random rand1 = new Random();
Int32 Fight = rand1.Next(1, 11);
label4.Text = Convert.ToString(10);
if (Fight <= t)
{
label3.Text = Convert.ToString(J);
}
else
{
txtDialogBox.Text = ("No Fight in this room");
}
}
private void Attack_Click(object sender, EventArgs e)
{
Random rand2 = new Random();
Int32 J = 10;
Int32 Attack = rand2.Next(1, 4);
//opponents hp bar goes down by 1
J --;
label3.Text = Convert.ToString(J);
// chance for your hp bar to go down
if (Attack >= y)
{
label4.Text = Convert.ToString(t);
t--;
}
}
//Additionally, just for learning
public int32 J=10
//would mean anything, even things out of this class can access code in the class.
protected int32 J=10
//would mean anything that *Implements* this has access to this variable (something called an assembly)
this way the class knows this is a private variable to this class that any class function can use. learn about access modifiers and find the one that works. Typically it is safe to make all class variables private, and use public access functions to modify them.
Upvotes: 0
Reputation: 930
You have to take off few variable and declare them globally to be used by all methods.
That should look something like this
public partial class Form1 : Form
{
Int32 t = 6;
Int32 J = 10;
public Form1()
{
InitializeComponent();
Random rand1 = new Random();
Int32 Fight = rand1.Next(1, 11);
label4.Text = Convert.ToString(J);
if (Fight <= t)
label3.Text = Convert.ToString(J);
else
txtDialogBox.Text = ("No Fight in this room");
}
private void Attack_Click(object sender, EventArgs e)
{
Random rand2 = new Random();
Int32 Attack = rand2.Next(1, 4);
Int64 y = 1;
//opponents hp bar goes down by 1
J--;
label3.Text = Convert.ToString(J);
// chance for your hp bar to go down
if (Attack >= y)
{
label4.Text = Convert.ToString(t);
t--;
}
}
}
Upvotes: 0
Reputation: 26281
Local variables declared on a method (or constructor, in your case), can only be accessed by the method itself.
In C#, the openning and closing brackets ({ and }) defines a block.
Every block has its own scope. When you define a variable in a block, you are defining it in the block's scope.
Now, here is the most important part, blocks inherit the scopes of parent blocks, but cannot access the scope of child blocks or any other external blocks.
For example,
public void SomeMethod() { //This is the method's scope
int someVar = 10; //We define a variable on the method's scope
//We can access "someVar" within the method
if(someVar < 10) { //This is the if scope. It inherits the parent method scope.
someVar = someVar + 1; //Can access parent's scope
bool valid = true; //We define a variable on the if's scope
}
valid = false; //We can't do this. "valid" was defined in a child scope (if).
//We can only access our scope or parent's scope.
}
Furthermore, the method cannot access other method's variables, because those are external scopes.
This is what is happening to you.
You are trying to access the int
variables from another method, in your Click
event handler. You need to either define those variables globally (on the class's scope, aka parent scope), or define new ones in the method's local scope.
I hope I made myself clear enough.
Upvotes: 2