4 Replies - 210 Views - Last Post: 12 May 2020 - 07:58 AM Rate Topic: -----

#1 HappyNewb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 28-April 20

How do i use a variable in a loop later on in my program?

Posted 12 May 2020 - 05:41 AM

I get user input in a while loop, but i cant use that variable outside of the loop.

i thought if i created a variable at the start of the class it would store it throughout the program but its not.

here is my code:

using System;
using System.Security.Cryptography.X509Certificates;

namespace MuliplicationTable
{

    class Program
    {
        int userNumber;

        static void Main(string[] args)
        {
            bool hasValidNumber = false;
            while (!hasValidNumber)
            {
                Console.WriteLine("Enter number between 1 and 12:");

                int userNumber = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine(userNumber);
                if (userNumber >= 1 && userNumber <= 12)
                {
                    hasValidNumber = true;
                } 
                else
                {
                    Console.WriteLine("That is not a valid number between 1 and 12:");
                }
            }

            Console.Write(userNumber);    
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: How do i use a variable in a loop later on in my program?

#2 HappyNewb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 28-April 20

Re: How do i use a variable in a loop later on in my program?

Posted 12 May 2020 - 05:56 AM

ok i realize that this:

int userNumber;

has to be

public static int userNumber;

but how can i make the static userNumber variable the same value as the userNumber variable in the local space?

i thought thats what the this keyword is for, but it doesnt work.
Was This Post Helpful? 0
  • +
  • -

#3 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1556
  • View blog
  • Posts: 2,187
  • Joined: 24-August 11

Re: How do i use a variable in a loop later on in my program?

Posted 12 May 2020 - 06:51 AM

It doesn't have to be static, and you don't need "this".

EDIT: Actually, it does need to be static, since you're using it from the static Main method.

Look at lines 9 and 18. You're declaring an integer field named userNumber on line 9, and then you're declaring an integer variable named userNumber on line 18, rather than just assigning a value to the field you already created. You don't use a type declaration (i.e. int) when you're assigning a value, you just use the field/property/variable that you've already created.

static int myVariable;

static void Main(string[] args)
{
    if (x == y)
    {
        myVariable = 1;
    }
    else
    {
        myVariable = 2;
    }
}



As for the public part - you only need to make userNumber public if it needs to be exposed outside of your Program class.

This post has been edited by h4nnib4l: 12 May 2020 - 07:11 AM

Was This Post Helpful? 1
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: How do i use a variable in a loop later on in my program?

Posted 12 May 2020 - 06:58 AM

It's all about that scope.

So you have this here:

09        int userNumber;

.. but you create an entirely different version inside the scope of the for loop here:
18                int userNumber = Convert.ToInt32(Console.ReadLine());


from the 'int' part off line 18 and you will no longer be creating a new variable in the scope of teh while loop, but using the variable in line 09.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7391
  • View blog
  • Posts: 24,952
  • Joined: 05-May 12

Re: How do i use a variable in a loop later on in my program?

Posted 12 May 2020 - 07:58 AM

Time for our OP to learn about scopes.

Out of curiosity, what book or tutorial are you using to learn C#? Variable scoping is usually introduced shortly after introducing the concept of flow control and methods.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1