none
CheckBox string value can't be accessed RRS feed

  • Question

  • I'm trying to make a winform app using .NET framework. I have a check box, and I'm trying to use it's information in a "save" buttton. Here is the code:
            public void isAliveCheckBox_CheckedChanged(object sender, EventArgs e)
            {
                string aliveStatus;
                if (isAliveCheckBox.Checked)
                {
                    aliveStatus = "is alive";
                }
                else
                    aliveStatus = "is dead";

            }But when I'm trying to use the aliveStatus here:  userListBox.Items.Add(string.Format("{0} {1} is {2} and {3}", firstNameTextBox.Text, lastNameTextBox.Text, ageDomainUpDown.Text, aliveStatus.Text));

    It says that the name aliveStatus does not exist in the current context. Any advice?

    Saturday, October 12, 2019 9:39 AM

Answers

  • "aliveStatus" is a local variable that you declared inside the method isAliveCheckBox_CheckedChanged. Therefore, it "dissappears" (goes out of scope) when you exit the method, and you can't use it from anywhere else.

    If you move the other code so that it is within the scope of the variable, then you will be able to use it. But note that you should write only "aliveStatus" which is the name of the variable, not "aliveStatus.Text". There is no ".Text" in a variable of type string.

    • Marked as answer by horatiu_alex Saturday, October 12, 2019 10:02 AM
    Saturday, October 12, 2019 9:54 AM
    Moderator
  • If I want to acces again aliveStatus, let's say in a add user button, how can i do this?

    You move the declaration out of the method.

    For example, let's imagine that the class that contains both the checkboxchanged method and the adduser click method is called "Form1". If you declare the variable in Form1 then it cam be acessed by both methods:

    public class Form1 : Form
    {
        private string aliveStatus;
        //...
        public void isAliveCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            // here you can access aliveStatus (don't declare it again here!!!)
        }
        //...
        public void addUser_Click(object sender, EventArgs e)
        {
            // here you can also access aliveStatus
        }
        //...
    }

    • Marked as answer by horatiu_alex Saturday, October 12, 2019 2:23 PM
    Saturday, October 12, 2019 2:13 PM
    Moderator

All replies

  • Fixed it myself! I just moved the code under the save button. If you have a better ideea please write it here, thanks!
    Saturday, October 12, 2019 9:54 AM
  • "aliveStatus" is a local variable that you declared inside the method isAliveCheckBox_CheckedChanged. Therefore, it "dissappears" (goes out of scope) when you exit the method, and you can't use it from anywhere else.

    If you move the other code so that it is within the scope of the variable, then you will be able to use it. But note that you should write only "aliveStatus" which is the name of the variable, not "aliveStatus.Text". There is no ".Text" in a variable of type string.

    • Marked as answer by horatiu_alex Saturday, October 12, 2019 10:02 AM
    Saturday, October 12, 2019 9:54 AM
    Moderator
  • If I want to acces again aliveStatus, let's say in a add user button, how can i do this?
    Saturday, October 12, 2019 10:02 AM
  • If I want to acces again aliveStatus, let's say in a add user button, how can i do this?

    You move the declaration out of the method.

    For example, let's imagine that the class that contains both the checkboxchanged method and the adduser click method is called "Form1". If you declare the variable in Form1 then it cam be acessed by both methods:

    public class Form1 : Form
    {
        private string aliveStatus;
        //...
        public void isAliveCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            // here you can access aliveStatus (don't declare it again here!!!)
        }
        //...
        public void addUser_Click(object sender, EventArgs e)
        {
            // here you can also access aliveStatus
        }
        //...
    }

    • Marked as answer by horatiu_alex Saturday, October 12, 2019 2:23 PM
    Saturday, October 12, 2019 2:13 PM
    Moderator