0

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.

4
  • If you have code that doesn't work and you want us to tell you why, perhaps you should show us that code. Commented Nov 13, 2014 at 2:59
  • 1
    is there a reason you're discerning between Int32 and Int64? Looks like you should just be using int Commented Nov 13, 2014 at 3:04
  • to my knowledge which is (little) there is no difference between Int32 Int64 and Int and the code is there bra Commented Nov 13, 2014 at 4:41
  • There is a difference, Int32 has a size of 4 bytes, while Int64 has a size of 8 bytes. Now, int is just an alias for saying Int32, so you got that right about int and Int32 being equal. Commented Nov 13, 2014 at 12:08

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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--;
        }
    }
}

Comments

0

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.

1 Comment

This won't compile. Private should be private (lowercase)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.