Evil Burgers
Evil Burgers

Reputation: 25

c# Using strings on the outside of Blocks to edit it

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string aaxe = null;
    string apick = null;
    string asho = null;
    string acan = null;
    string aknife = null;
    string akey = null;
    string atre = null;

When I click on the Axe(aaxe). To edit the string of atre to "y" instead of null. But when I try to do that it doesn't find atre inside of the aaxe block. So how do I fix that?

public void Axe_Click(object sender, EventArgs e)
{
    if (aaxe == null)
    {
        richTextBox1.AppendText("You Don't Have An Axe\r\n");
    }

    if (aaxe == "y")
    {
        richTextBox1.AppendText("You Used your Axe\r\n");
    }
}

And here's the "atre" code

    public void Treasure_Click(object sender, EventArgs e)
    {
        if (atre == null)
        {
            richTextBox1.AppendText("You Haven't Found the Treasure Yet!\r\n");
        string aaxe = "y"

And it can't find aaxe!

        }
        if (atre == "y")
        {
            richTextBox1.AppendText("You Found The Treasure!!!\r\n");
            richTextBox1.AppendText("The End Of The Adventure!");
        }
    }

Upvotes: 0

Views: 49

Answers (2)

Ben
Ben

Reputation: 2523

You're re-declaring the variable. That's your issue. When you declare a variable, you can set the type:

string aaxe = null;
string atre = null;

When you call a variable, you only need to call it by the variable name:

aaxe = "y";

Like so:

public partial class Form1 : Form
{
    // Set the variables here
    string aaxe = null;
    string atre = null;

    public Form1()
    {
        InitializeComponent();
    }

    public void Treasure_Click(object sender, EventArgs e)
    {
        if (atre == null)
        {
            richTextBox1.AppendText("You Haven't Found the Treasure Yet!\r\n");
            // When you call a variable, you don't need to add the 'string' type. 
            aaxe = "y"
        }
        if (atre == "y")
        {
            richTextBox1.AppendText("You Found The Treasure!!!\r\n");
            richTextBox1.AppendText("The End Of The Adventure!");
        }
    }

    public void Axe_Click(object sender, EventArgs e)
    {
        // Then, when you check the value of aaxe, it will = "y".
        if (aaxe == null)
        {
            richTextBox1.AppendText("You Don't Have An Axe\r\n");
        }
        if (aaxe == "y")
        {
            richTextBox1.AppendText("You Used your Axe\r\n");
        }
    }
}

Upvotes: 1

Infinity Challenger
Infinity Challenger

Reputation: 1140

You declare the variable aaxe twice in different scopes : One is the member level and the other local variable.

Please change the second string aaxe to aaxe = "y"; without string type.

 string aaxe = "y";

to

  aaxe = "y";

Upvotes: 0

Related Questions