Thread: Quick question

Results 1 to 6 of 6
  1. #1
    Meerster's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Turn Around =)
    Posts
    41
    Reputation
    22
    Thanks
    6
    My Mood
    Yeehaw

    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?
    Last edited by Meerster; 01-15-2015 at 01:09 PM.

    [/CENTER]

  2. #2
    Jorndel's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    Norway
    Posts
    8,676
    Reputation
    905
    Thanks
    19,112
    My Mood
    Angelic
    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: https://msdn.microsof*****m/en-us/library/0yd65esw.aspx
    Last edited by Jorndel; 01-16-2015 at 08:32 AM.

     
    Contributor 01.27.2012 - N/A
    Donator 07-17-2012 - Current
    Editor/Manager 12-16-12 - N/A
    Minion 01-10-2013 - 07.17.13
    Former Staff 09-20-2012 - 01-10-2013 / 07-17-2013 - Current
    Cocksucker 20-04-2013 - N/A

  3. #3
    zTwix0R's Avatar
    Join Date
    May 2012
    Gender
    male
    Posts
    96
    Reputation
    10
    Thanks
    6
    My Mood
    Aggressive
    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*);

  4. #4
    Disturbed's Avatar
    Join Date
    Feb 2009
    Gender
    male
    Posts
    10,472
    Reputation
    1267
    Thanks
    2,587
    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;
                }
            }
    Last edited by Disturbed; 01-16-2015 at 08:36 PM.


  5. #5
    zTwix0R's Avatar
    Join Date
    May 2012
    Gender
    male
    Posts
    96
    Reputation
    10
    Thanks
    6
    My Mood
    Aggressive
    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.
    Last edited by zTwix0R; 01-17-2015 at 03:00 PM.

  6. #6
    ZER0MEM0RY's Avatar
    Join Date
    Feb 2015
    Gender
    male
    Location
    \\\\.\\PhysicalDrive0
    Posts
    94
    Reputation
    10
    Thanks
    4,217
    My Mood
    Cold
    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

Similar Threads

  1. just a quick question about weapons
    By depkillz in forum WarRock - International Hacks
    Replies: 1
    Last Post: 03-13-2007, 11:48 PM
  2. quick question WPE
    By svalkur in forum WarRock - International Hacks
    Replies: 1
    Last Post: 07-24-2006, 11:26 PM
  3. quick question
    By ace76543 in forum Art & Graphic Design
    Replies: 11
    Last Post: 05-16-2006, 09:53 PM
  4. Quick Question
    By mostwanted in forum WarRock - International Hacks
    Replies: 1
    Last Post: 02-05-2006, 09:50 PM
  5. Quick Question..Please Answer
    By ypg_gamer in forum WarRock - International Hacks
    Replies: 4
    Last Post: 01-04-2006, 10:32 AM