HelpQuick question

Posts 16 of 6 · Page 1 of 1
Quick question
So, I'm trying to get it so a user inputs a number into a textbox and outputs a messagebox. But, I keep getting the error; The name 'value does not exist in the current context. Any idea"
Code:
        private void btnEnter_Click(object sender, EventArgs e)
        {

            // Strings MessageBox's
            string a, c, d, f, g, h, i, j, k, l;

            // Assign Values
            a = "You Got it! Heres your next clue:\nCLUE";
            c = "You Got it! Heres your next clue:\nCLUE";
            d = "You Got it! Heres your next clue:\nCLUE";
            f = "You Got it! Heres your next clue:\nCLUE";
            g = "You Got it! Heres your next clue:\nCLUE";
            h = "You Got it! Heres your next clue:\nCLUE";
            i = "You Got it! Heres your next clue:\nCLUE";
            j = "You Got it! Heres your next clue:\nCLUE";
            k = "You Got it! Heres your next clue:\nCLUE";
            l = "You Got it! Heres your next clue:\nCLUE";

                int userNumber = int.Parse(txtEnter.Text);
                switch (value)
                {
                    case "5849":
                        return MessageBox.Show(a);
                        break;
                    case "4865":
                        return MessageBox.Show(c);
                        break;
                    default:
                        break;
                }
            }
EDIT: Figured it out. How would I go about adding a try catch exception?
Quote Originally Posted by Meerster View Post
So, I'm trying to get it so a user inputs a number into a textbox and outputs a messagebox. But, I keep getting the error; The name 'value does not exist in the current context. Any idea"
Code:
        private void btnEnter_Click(object sender, EventArgs e)
        {

            // Strings MessageBox's
            string a, c, d, f, g, h, i, j, k, l;

            // Assign Values
            a = "You Got it! Heres your next clue:\nCLUE";
            c = "You Got it! Heres your next clue:\nCLUE";
            d = "You Got it! Heres your next clue:\nCLUE";
            f = "You Got it! Heres your next clue:\nCLUE";
            g = "You Got it! Heres your next clue:\nCLUE";
            h = "You Got it! Heres your next clue:\nCLUE";
            i = "You Got it! Heres your next clue:\nCLUE";
            j = "You Got it! Heres your next clue:\nCLUE";
            k = "You Got it! Heres your next clue:\nCLUE";
            l = "You Got it! Heres your next clue:\nCLUE";

                int userNumber = int.Parse(txtEnter.Text);
                switch (value)
                {
                    case "5849":
                        return MessageBox.Show(a);
                        break;
                    case "4865":
                        return MessageBox.Show(c);
                        break;
                    default:
                        break;
                }
            }
EDIT: Figured it out. How would I go about adding a try catch exception?
Code:
//This add's an check if the value we try to "convert" can be converted to an Integer
            if (int.TryParse(textBox1.Text, out userNumber))
            {
                if (userNumber == 5849)
                    MessageBox.Show(a);
                else if (userNumber == 4865)
                    MessageBox.Show(c);
                else MessageBox.Show("Invalid Number");
            }
            else MessageBox.Show("Something went wrong...");

            //-----------------------------------------------
            //We're not using any Exception here

            try
            {
                int.Parse(textBox1.Text);
            }
            catch
            {
                MessageBox.Show("Error");
            }
To look at Exception's read more here: http://msdn.microsoft.com/en-us/library/0yd65esw.aspx
Quote Originally Posted by Meerster View Post
So, I'm trying to get it so a user inputs a number into a textbox and outputs a messagebox. But, I keep getting the error; The name 'value does not exist in the current context. Any idea"
Code:
        private void btnEnter_Click(object sender, EventArgs e)
        {

            // Strings MessageBox's
            string a, c, d, f, g, h, i, j, k, l;

            // Assign Values
            a = "You Got it! Heres your next clue:\nCLUE";
            c = "You Got it! Heres your next clue:\nCLUE";
            d = "You Got it! Heres your next clue:\nCLUE";
            f = "You Got it! Heres your next clue:\nCLUE";
            g = "You Got it! Heres your next clue:\nCLUE";
            h = "You Got it! Heres your next clue:\nCLUE";
            i = "You Got it! Heres your next clue:\nCLUE";
            j = "You Got it! Heres your next clue:\nCLUE";
            k = "You Got it! Heres your next clue:\nCLUE";
            l = "You Got it! Heres your next clue:\nCLUE";

                int userNumber = int.Parse(txtEnter.Text);
                switch (value)
                {
                    case "5849":
                        return MessageBox.Show(a);
                        break;
                    case "4865":
                        return MessageBox.Show(c);
                        break;
                    default:
                        break;
                }
            }
EDIT: Figured it out. How would I go about adding a try catch exception?
"return MessageBox.Show(c);"

Why are you returning anything at all?
This function handles a button click, so you don't need to return anything. Plus, MessageBox.Show() is a method that returns user's response to MessageBox(weather you clicked Ok, or Yes, or No, etc).

Replace the return statements with just MessageBox.Show(*message you want to display*);
Quote Originally Posted by zTwix0R View Post
"return MessageBox.Show(c);"

Why are you returning anything at all?
This function handles a button click, so you don't need to return anything. Plus, MessageBox.Show() is a method that returns user's response to MessageBox(weather you clicked Ok, or Yes, or No, etc).

Replace the return statements with just MessageBox.Show(*message you want to display*);

I've occasionally used a return statement to stop a method's execution many times in complicated input validation. If you have a lot of code coming after the point where you need to berate the user for typing out "forty-five", it becomes much less complicated to use return than to nest everything in if/else if/else blocks (if you can't/don't want to just pass everything to a separate method and call it upon determining all input is valid).

Not that he should be using it here. He clearly doesn't need to, but there is some use to calling return in a void method under certain circumstances.
@Meerster @Jorndel

Code:
        private void btnEnter_Click(object sender, EventArgs e)
        {
            //Base message string.
            string strBaseMessage = "You Got it! Heres your next clue:\n";


            int intUserNumber;


            //If the user has some invalid input, tell them and stop exection of the method.
            if (!int.TryParse(txtEnter.Text, out intUserNumber))
            {
                MessageBox.Show("Ya dun goofed");
                return; 
            }


            switch (value)
            {
                case "5849":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                case "4865":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                default:
                    break;
            }
        }
OR

Code:
        private void btnEnter_Click(object sender, EventArgs e)
        {
            int intUserNumber;


            //Check for valid user input
            if (int.TryParse(txtEnter.Text, out intUserNumber))
            {
                ProceedWithInput(intUserNumber);
            }
            else
            {
                MessageBox.Show("Ya dun goofed");
            }


        }


        private void ProceedWithInput(int intUserInput)
        {
            //Base message string.
            string strBaseMessage = "You Got it! Heres your next clue:\n";


            switch (intUserInput)
            {
                case "5849":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                case "4865":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                default:
                    break;
            }
        }
Quote Originally Posted by Disturbed View Post



I've occasionally used a return statement to stop a method's execution many times in complicated input validation. If you have a lot of code coming after the point where you need to berate the user for typing out "forty-five", it becomes much less complicated to use return than to nest everything in if/else if/else blocks (if you can't/don't want to just pass everything to a separate method and call it upon determining all input is valid).

Not that he should be using it here. He clearly doesn't need to, but there is some use to calling return in a void method under certain circumstances.
@Meerster @Jorndel

Code:
        private void btnEnter_Click(object sender, EventArgs e)
        {
            //Base message string.
            string strBaseMessage = "You Got it! Heres your next clue:\n";


            int intUserNumber;


            //If the user has some invalid input, tell them and stop exection of the method.
            if (!int.TryParse(txtEnter.Text, out intUserNumber))
            {
                MessageBox.Show("Ya dun goofed");
                return; 
            }


            switch (value)
            {
                case "5849":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                case "4865":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                default:
                    break;
            }
        }
OR

Code:
        private void btnEnter_Click(object sender, EventArgs e)
        {
            int intUserNumber;


            //Check for valid user input
            if (int.TryParse(txtEnter.Text, out intUserNumber))
            {
                ProceedWithInput(intUserNumber);
            }
            else
            {
                MessageBox.Show("Ya dun goofed");
            }


        }


        private void ProceedWithInput(int intUserInput)
        {
            //Base message string.
            string strBaseMessage = "You Got it! Heres your next clue:\n";


            switch (intUserInput)
            {
                case "5849":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                case "4865":
                    return MessageBox.Show(strBaseMessage + "CLUE");
                    break;
                default:
                    break;
            }
        }
Well I agree, I've used it a couple of times too. But MessageBox.Show() returns a DialogResult, so it wouldn't work here.

This, however, might work:
Code:
switch (intUserInput)
            {
                case "5849":
                    MessageBox.Show(strBaseMessage + "CLUE");
                    return;
                    break;
                case "4865":
                    MessageBox.Show(strBaseMessage + "CLUE");
                    return;
                    break;
                default:
                    break;
            }
But you are right, there are some cases where it's easier to do that.

EDIT: The "break" statement isn't even needed since the method never reaches it, you might as well just delete it.
First off, you should use a string array: string[] msgBox_txt = {"some text", "more text"};

and use messageBox.Show(msgBox_txt[1]); //to display text
Posts 16 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?