2

So i started learning c# a few days ago and i started making a simple multiplier program:

using System; 
namespace learning_the_syntax
{

    public class GlobalVariables //this class stores all global variables i want to use. Local variables will be stored inside their function/method
    {
        int num01;
        int num02;
    }

    class MainClass
    {
         public static void Main(string[] args)  //This is a function/method named "Main". It is called when the program starts.
         {
             Console.WriteLine("Type a number to be multipied: ");
             num01 = Console.ReadLine();



         }
    }

}

I created a public class to store my global variables but when i tried to use the variable num01 in my Main class, it shows an error message stating that num01 does not exist in its current context. Can anybody help please? Thank you.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
Jordan Evans
  • 51
  • 2
  • 4
  • 3
    Please, stop with the global variables already. They are bad and, if you learn them now, you'll have to unlearn them later. Instead, read up on passing values between methods/functions via parameters. – David Arno Jun 29 '15 at 21:13
  • 2
    @DavidArno It's not universally bad. There are situations where its appropriate, and situations where it's not. It is true that many new programmers use it when they shouldn't, as a crutch for not storing data more appropriately. That's a very important distinction. If global state was universally bad it wouldn't be in the language to begin with. It seems pretty clear that the data in question here shouldn't be stored globally though. – Servy Jun 29 '15 at 21:14
  • 1
    Jordan, store the numbers as local variables of that method. If you're calling other methods that need that value, pass them in as parameters. Always strive to keep data scoped as narrowly as possible; expose data to as few locations as you can possibly manage. – Servy Jun 29 '15 at 21:16
  • @Servy, What? "It's not universally bad"??!?????? I am really seriously shocked you could say that. There are absolutely no use cases where global variables are the right answer. Ever. They make testing a nightmare, and, after readability, being testable is the single most important aspect of any piece of code. – David Arno Jun 29 '15 at 21:17
  • @DavidArno If the data being stored is conceptually global, then it's absolutely fine. If you want to create a variable for, say, the value of `PI`, the command line arguments to the program that's currently running, the current directory, etc. then these are things that are conceptually expected to be values shared to the entire program, and not scoped to any smaller. It should generally be quite rare to see it though. What you shouldn't ever be doing is storing data globally unless it would be non-nonsensical for there to be multiple values for that data within the program. – Servy Jun 29 '15 at 21:21
  • @Servy, `PI` is not "conceptually global"; it's constant. The vast majority of the `Environment` is an example of why non-constant global state is bad though. Such data should be injected into the main method, not accessed via the service locator anti-pattern as it makes eg testing a linux mono environment on a windows platform harder than it need be. Globals are bad. – David Arno Jun 29 '15 at 21:26

1 Answers1

1

In order to access members outside the class you must declare them public, so you would have:

public int num01;
public int num02;

If you want the variables to be global in the program you should consider making them static:

public static int num01;
public static int num02;

Then you could access them using the syntax:

GlobalVariables.num01;
GlobalVariables.num01;
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
SimonaB
  • 67
  • 1
  • 5
  • 2
    It's good answer technically speaking. However it's probably not the best way to do so - via comments to the first post. – jjczopek Jun 29 '15 at 21:33
  • Thank you very much for your answer. Although other people are saying that global variables are bad. As a new programmer im extremely confused on what to believe but at least i know how to do them now so thanks again. – Jordan Evans Jul 06 '15 at 19:38