none
Tutorial 2: Create a timed math quiz - C# error RRS feed

  • Question

  • I am using VS 2015 and C#.  I am attempting to learn C#.  In am in Tutorial 2:Create a timed math quiz.  I get error

    Error	CS0103	The name 'minusLeftLabel' does not exist in the current context	WindowsFormsApplication1

    Below is the code:

     minusLeftLabel.Text = minuend.ToString();
    The label minusLeftLabel is defined in my form in the designer. Spelled correctly.   What am I doing incorrect.  Any info would be appreciated.   A am a New Bee in C#

     

    Edcal


    Wednesday, July 11, 2018 1:05 AM

Answers

  • Additional code as requested:

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            // Note my new code start here
    
            // Create a Random object called randomizer 
            // to generate random numbers.
            Random randomizer = new Random();
    
    
            // These integer variables store the numbers 
            // for the addition problem. 
            int addend1;
            int addend2;
    
            // These integer variables store the numbers 
            // for the subtraction problem. 
            int minuend;
            int subtrahend;
    
            // This integer variable keeps track of the 
            // remaining time.
            int timeLeft;
    
    
    
            /// <summary>
            /// Start the quiz by filling in all of the problems
            /// and starting the timer.
            /// </summary>
            public void StartTheQuiz()
            {
                // Fill in the addition problem.
                // Generate two random numbers to add.
                // Store the values in the variables 'addend1' and 'addend2'.
                addend1 = randomizer.Next(51);
                addend2 = randomizer.Next(51);
    
                // Convert the two randomly generated numbers
                // into strings so that they can be displayed
                // in the label controls.
                plusLeftLabel.Text = addend1.ToString();
                plusRightLabel.Text = addend2.ToString();
    
                // 'sum' is the name of the NumericUpDown control.
                // This step makes sure its value is zero before
                // adding any values to it.
                sum.Value = 0;
    
                // Fill in the subtraction problem.
                minuend = randomizer.Next(1, 101);
                subtrahend = randomizer.Next(1, minuend);
                minusLeftLabel.Text = minuend.ToString();
    
    

    Show (copy & paste) the code from Form1.Designer.cs where it has:

    ...
    this.minusLeftLabel = new System.Windows.Forms.Label();
    ...

    - Wayne

    Wednesday, July 11, 2018 2:33 AM
  • Hi Edcal,

    Please set the Name property of the label to minusLeftLabel with the following way in the Properties view:

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 11, 2018 2:56 AM

All replies

  • We'd need to see more of your code ... is that code in another class that is "outside" of WindowFormsApplication1? If so, that class doesn't know anything about the controls that you have on your Form, unless you structure your class differently.

    Anyway, show some more of your code and maybe we can figure it out ...


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, July 11, 2018 1:26 AM
    Moderator
  • Additional code as requested:

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            // Note my new code start here
    
            // Create a Random object called randomizer 
            // to generate random numbers.
            Random randomizer = new Random();
    
    
            // These integer variables store the numbers 
            // for the addition problem. 
            int addend1;
            int addend2;
    
            // These integer variables store the numbers 
            // for the subtraction problem. 
            int minuend;
            int subtrahend;
    
            // This integer variable keeps track of the 
            // remaining time.
            int timeLeft;
    
    
    
            /// <summary>
            /// Start the quiz by filling in all of the problems
            /// and starting the timer.
            /// </summary>
            public void StartTheQuiz()
            {
                // Fill in the addition problem.
                // Generate two random numbers to add.
                // Store the values in the variables 'addend1' and 'addend2'.
                addend1 = randomizer.Next(51);
                addend2 = randomizer.Next(51);
    
                // Convert the two randomly generated numbers
                // into strings so that they can be displayed
                // in the label controls.
                plusLeftLabel.Text = addend1.ToString();
                plusRightLabel.Text = addend2.ToString();
    
                // 'sum' is the name of the NumericUpDown control.
                // This step makes sure its value is zero before
                // adding any values to it.
                sum.Value = 0;
    
                // Fill in the subtraction problem.
                minuend = randomizer.Next(1, 101);
                subtrahend = randomizer.Next(1, minuend);
                minusLeftLabel.Text = minuend.ToString();
                minusRightLabel.Text = subtrahend.ToString();
                difference.Value = 0;
    
                // Start the timer.
                timeLeft = 30;
                timeLabel.Text = "30 seconds";
                timer1.Start();
    
            }


    Edcal

    Wednesday, July 11, 2018 2:23 AM
  • I am using VS 2015 and C#.  ... In am in Tutorial 2:Create a timed math quiz. 

    Presumably you are referring to this:

    Tutorial 2: Create a Timed Math Quiz
    https://msdn.microsoft.com/en-us/library/dd492172.aspx

    - Wayne

    Wednesday, July 11, 2018 2:24 AM
  • Additional code as requested:

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            // Note my new code start here
    
            // Create a Random object called randomizer 
            // to generate random numbers.
            Random randomizer = new Random();
    
    
            // These integer variables store the numbers 
            // for the addition problem. 
            int addend1;
            int addend2;
    
            // These integer variables store the numbers 
            // for the subtraction problem. 
            int minuend;
            int subtrahend;
    
            // This integer variable keeps track of the 
            // remaining time.
            int timeLeft;
    
    
    
            /// <summary>
            /// Start the quiz by filling in all of the problems
            /// and starting the timer.
            /// </summary>
            public void StartTheQuiz()
            {
                // Fill in the addition problem.
                // Generate two random numbers to add.
                // Store the values in the variables 'addend1' and 'addend2'.
                addend1 = randomizer.Next(51);
                addend2 = randomizer.Next(51);
    
                // Convert the two randomly generated numbers
                // into strings so that they can be displayed
                // in the label controls.
                plusLeftLabel.Text = addend1.ToString();
                plusRightLabel.Text = addend2.ToString();
    
                // 'sum' is the name of the NumericUpDown control.
                // This step makes sure its value is zero before
                // adding any values to it.
                sum.Value = 0;
    
                // Fill in the subtraction problem.
                minuend = randomizer.Next(1, 101);
                subtrahend = randomizer.Next(1, minuend);
                minusLeftLabel.Text = minuend.ToString();
    
    

    Show (copy & paste) the code from Form1.Designer.cs where it has:

    ...
    this.minusLeftLabel = new System.Windows.Forms.Label();
    ...

    - Wayne

    Wednesday, July 11, 2018 2:33 AM
  • Hi Edcal,

    Please set the Name property of the label to minusLeftLabel with the following way in the Properties view:

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 11, 2018 2:56 AM
  • I had the L in Left and Label in uppercase in the code and the properties.   When I change to lowercase it work.   Thanks very much.   I will close.   Thanks again.

    Edcal

    Wednesday, July 11, 2018 3:36 AM
  • Ed,

    Here's a helpful tip for the next bit of code that you try to write:  make use of "this" .... if you had started typing:

    this.minus

    before you even got that far with your typing, Intellisense would have kicked in, showing you the properties ... which helps you to choose the correct property *and* keep the spelling correct.   =0)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, July 11, 2018 4:48 AM
    Moderator