0

I've made a class called MyVarsand thats just there to basically store the varibales for use thorughout the entire program. I have tried adding the variables to the same class in a different method but the results are the same. I bascially want to make some variables global so i can use them.

 class MyVars
{ 
    public string nameFirst;
    public string nameLast;
    public int age;
    public double height;
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter first name:");
        nameFirst = Console.ReadLine();
        Console.WriteLine("Enter last name:");
        nameLast = Console.ReadLine();
        Console.WriteLine("Enter age:");
        age = Console.ReadLine();
    }

That's basically the code I've made. Is there a way to make the variables be able to be used in different methods, I don't care about classes yet as I don't mind re-making them for other classes.

  • make your call and variables static – Nouman Bhatti Apr 06 '20 at 01:56
  • Although the fields in MyVars are public, there is no instance of MyVars in your Program.main method. So you'd either have to have an instance of MyVars (recommended) or go for the static approach described by @NoumanBhatti (not recommended IMHO). Also, you might want to take a look around here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/ and read up on Classes, Members etc. – Martin Klinke Apr 06 '20 at 10:18

4 Answers4

0

Is this what you are looking for?

class Program
{
    public static string nameFirst;
    public static string nameLast;
    public static int age;
    public static double height;

    static void Main(string[] args)
    {
        Console.WriteLine("Enter first name:");
        nameFirst = Console.ReadLine();
        Console.WriteLine("Enter last name:");
        nameLast = Console.ReadLine();
        Console.WriteLine("Enter age:");
        age = Console.ReadLine();
    }
}

Generally speaking, a main purpose of classes is to group together your data/variables together with the methods that use or change the data/variables.

xdhmoore
  • 8,935
  • 11
  • 47
  • 90
  • This does not work. I tried what you said but the same error is appearing. It says that ' Error CS0120 An object reference is required for the non-static field, method, or property 'Program.nameFirst' ' –  Apr 05 '20 at 19:47
  • That makes sense. The static method can't access a non-static variable. I've changed my answer to make all the variables static as well. – xdhmoore Apr 05 '20 at 19:51
0

You need create an instance of MyVars in Main method and add public keyword before defined class MyVars

public class MyVars
{ 
    public string nameFirst;
    public string nameLast;
    public int age;
    public double height;
}
class Program
{
    static void Main(string[] args)
    {
        MyVars myVars = new MyVars();
        Console.WriteLine("Enter first name:");
        myVars.nameFirst = Console.ReadLine();
        Console.WriteLine("Enter last name:");
        myVars.nameLast = Console.ReadLine();
        Console.WriteLine("Enter age:");
        myVars.age = Console.ReadLine();
    }
}
Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
  • The program work now. Can i ask what the MyVars myVars = new MyVars(); does and how i should change it for other classes. So do i just change the name and make it lowercase for a different class? –  Apr 05 '20 at 19:59
  • @ExiusCain `MyVars myVars = new MyVars();` create an instance of `MyVars` and you can access to property and method in `MyVars` class. yes just change the name and make it lowercase for a different class – Farhad Zamani Apr 05 '20 at 20:08
0

This will help you.

  1. Create Separate MyVars.cs class. Better create a separate file as below.

enter image description here

public class MyVars
{ 
    public string NameFirst {get;set;}
    public string NameLast {get;set;}
    public int Age {get;set;}
    public double Height {get;set;}
}

Create new instance of MyVars class. You can pass this instance into other methods as a parameter. Or you can create a new instance of MyVars class in any other method.

class Program
{
    static void Main(string[] args)
    {  
        // New instance of MyVars 
        MyVars myVars = new MyVars ();

        Console.WriteLine("Enter first name:");
        myVars.NameFirst = Console.ReadLine();
        Console.WriteLine("Enter last name:");
        myVars.NameLast = Console.ReadLine();
        Console.WriteLine("Enter age:");
        myVars.Age = Console.ReadLine();
    }
}
0

I'm going to show you how to do this, but I'm going to also ask that you not do this. This is a path that will end up very very badly. Using global variables lead to code which is very very difficult to maintain. So... here's how to do it... but do not do this.

public static class MyVars
{
    public static string nameFirst;
    public static string nameLast;
    public static int age;
    public static double height;
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter first name:");
        MyVars.nameFirst = Console.ReadLine();
        Console.WriteLine("Enter last name:");
        MyVars.nameLast = Console.ReadLine();
        Console.WriteLine("Enter age:");
        MyVars.age = int.Parse(Console.ReadLine());
    }
}

Read this answer as to why you want to avoid global variables at (nearly) all costs:

https://stackoverflow.com/a/485020/5753629

Reginald Blue
  • 930
  • 11
  • 26