locked
Using variables in a class RRS feed

  • Question

  • I realise the title is kind of vague, but I couldn't think of a better way of putting it...

    Anyway, I'm trying to access and manipulate variables from one class within the main class, this is in main -

    Console.WriteLine("First Name?"); nUser.setfName(); Console.WriteLine("Last Name?"); nUser.setlName(); Console.WriteLine("Hello, " + nUser.fetchVar("fName") + " " + nUser.fetchVar("lName")); do { Console.WriteLine("Please enter your starting weight, in kilograms"); x = Console.ReadLine(); if(int.TryParse(x, out value)) { nUser.setsWeight(x); }else { Console.WriteLine("Please enter a whole number value. No decimal points."); } } while (nUser.isSet("sWeight")==false);

    I am indirectly assigning variables from the command line using functions like setfName and setlName within the nUser object. I am then fetching those variables using a fetchVar(variable name) function.

    And inside the User class, this is what those functions look like -

    public void setfName()
    	{
    	
    		fName = Console.ReadLine();
    		
    	}
    	public void setlName()
    	{
    	
    		lName = Console.ReadLine();
    		
    	}

    and

    public string fetchVar(string varType)
    	{
    		
    		switch (varType)
    		{
    		
    			case "fName":
    				return fName;
    				break;
    			
    			case "lName":
    
    				return lName;
    				break;
    			
    			case "sWeight":
    			
    				return Convert.ToString(sWeight);
    				break;
    			
    			case "gWeight":
    				return Convert.ToString(gWeight);
    				break;
    			
    			default:
    				return "error";
    			
    		}
    		
    	}

    I also have functions to check if those variables have been set -

    public void setsWeight(string x)
    	{
    		if (validate.weight(x) == true)
    		{
    			sWeight = Int32.Parse(x);
    		}
    		else
    		{
    			Console.WriteLine("Weight validation failed, weight must be 2 or 3 digits");
    		}
    		
    	}
    	public void setgWeight(string x)
    	{
    		if (validate.weight(x) == true)
    		{
    			gWeight = Int32.Parse(x);
    		}
    		else
    		{
    			Console.WriteLine("Weight validation failed, weight must be 2 or 3 digits");
    		}
    		
    	}

    Whilst it all seems to work I feel that I'm not doing this "properly". I am new to OOP and very much still learning, if anyone could help me out I would very much appreciate it.



    • Edited by Ebstenstor Monday, August 27, 2018 11:41 AM
    Monday, August 27, 2018 10:23 AM

Answers

  • A class should only expose methods and properties, not variables. Properties in C# have their own syntax. Your sample looks like Java.. In C#, e.g.

    namespace ConsoleCS
    {
        using System;
    
        public class Test
        {
            public string LastName { get; set; }
            public string FirstName { get; set; }
            private float weight;
            public float Weight
            {
                get
                {
                    return this.weight;
                }
    
                set
                {
                    if (value >= 0 && value < 1000)
                    {
                        this.weight = value;
                    }
                }
            }
    
            public override string ToString()
            {
                return $"{this.FirstName} {this.LastName}";
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Test test = new Test();
                Console.WriteLine("First Name?");
                test.FirstName = Console.ReadLine();
    
                Console.WriteLine("Last Name?");
                test.LastName = Console.ReadLine();
    
                Console.WriteLine($"Hello, {test}");
    
                Console.WriteLine("Please enter your starting weight, in kilograms");
                string weightText = Console.ReadLine();
                int weightInt;
                if (int.TryParse(weightText, out weightInt))
                {
                    test.Weight = weightInt;
                }
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    }
    

    • Marked as answer by Ebstenstor Monday, August 27, 2018 12:12 PM
    Monday, August 27, 2018 11:34 AM

All replies

  • A class should only expose methods and properties, not variables. Properties in C# have their own syntax. Your sample looks like Java.. In C#, e.g.

    namespace ConsoleCS
    {
        using System;
    
        public class Test
        {
            public string LastName { get; set; }
            public string FirstName { get; set; }
            private float weight;
            public float Weight
            {
                get
                {
                    return this.weight;
                }
    
                set
                {
                    if (value >= 0 && value < 1000)
                    {
                        this.weight = value;
                    }
                }
            }
    
            public override string ToString()
            {
                return $"{this.FirstName} {this.LastName}";
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Test test = new Test();
                Console.WriteLine("First Name?");
                test.FirstName = Console.ReadLine();
    
                Console.WriteLine("Last Name?");
                test.LastName = Console.ReadLine();
    
                Console.WriteLine($"Hello, {test}");
    
                Console.WriteLine("Please enter your starting weight, in kilograms");
                string weightText = Console.ReadLine();
                int weightInt;
                if (int.TryParse(weightText, out weightInt))
                {
                    test.Weight = weightInt;
                }
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    }
    

    • Marked as answer by Ebstenstor Monday, August 27, 2018 12:12 PM
    Monday, August 27, 2018 11:34 AM
  • Thankyou very much for this answer, I think I am starting to come to terms with C# way of thinking.

    Thanks again.

    Monday, August 27, 2018 12:13 PM