Home > OS >  how do i change the value of an object
how do i change the value of an object

Time:10-11

here is my main method:

string input = Console.ReadLine(); // 2006 2000 false 1000 500 Windows
List<string> inputs = input.Split(" ").ToList();
Computer one = new Computer(int.Parse(inputs[0]), int.Parse(inputs[1]), bool.Parse(inputs[2]), double.Parse(inputs[3]), double.Parse(inputs[4]), inputs[5]);
one.Print(); // 2006, 2000, false, 1000, 500, Windows
input = Console.ReadLine(); //changeOperatingSystem
if (input == "changeOperatingSystem")
{
string newOperationSystem = Console.ReadLine(); //Linux
Computer two = new Computer(newOperationSystem);
}
one.Print(); //2006, 2000, false, 1000, 500, Linux

the "//" marks what the input and the output look like. This is my Computer.cs:

private int years;
        private int price;
        private bool isNotebook;
        private double hardDiskMemory;
        private double freeMemory;
        private string operationSystem;
        private string newOperationSystem;

        public Computer(int years, int price, bool isNotebook, double hardDiskMemory, double freeMemory, string operationSystem)
        {
            Years = years;
            Price = price;
            IsNotebook = isNotebook;
            HardDiskMemory = hardDiskMemory;
            FreeMemory = freeMemory;
            OperationSystem = operationSystem;
        }
        public Computer(string newOperationSystem)
        {
            OperationSystem = newOperationSystem;
        }
        public int Years
        {
            get { return years; }
            set
            {
                years = value;
            }
        }
        public int Price
        {
            get { return price; }
            set
            {
                price = value;
            }
        }
        public bool IsNotebook
        {
            get { return isNotebook; }
            set
            {
                isNotebook = value;
            }
        }
        public double HardDiskMemory
        {
            get { return hardDiskMemory; }
            set
            {
                hardDiskMemory = value;
            }
        }
        public double FreeMemory
        {
            get { return freeMemory; }
            set
            {
                freeMemory = value;
            }
        }
        public string OperationSystem
        {
            get { return operationSystem; }
            set
            {
                operationSystem = value;
            }
        }
        
        public string NewOperationSystem
        {
            get { return NewOperationSystem; }
            set
            {
                newOperationSystem = value;
            }
        }

        public void Print()
        {
            Console.WriteLine($"{years}, {price}, {isNotebook}, {hardDiskMemory}, {FreeMemory}, {operationSystem}");
        }

what I want this program to do is whenever the input submitted by the user is "changeOperationSystem" the program gives the user another input where they submit the new Operation system for the pc. Then with the constructor, the Computer.Cs receives the string newOperationSystem, which then should replace the value operationSystem as I tried to do in the constructor: OperationSystem = newOperationSystem, but it doesn't work. I am stil learning classes so don't go hard on me.

CodePudding user response:

It seems to me you are trying to change one property of a class. All you have to do is use the property set method

one.OperationSystem = newOperationSystem;

and remove the property NewOperationSystem altogether.

Here is a bare bones skeleton code of instantiating a class with a constructor and then changing one of the properties

public class Foo
{
    private int _a;
    private string _b;
    private readonly float _c;

    public Foo(int a, string b, float c)
    {
        _a = a;
        _b = b;
        _c = c;
    }

    public int A { get => _a; set => _a = value; }
    public string B { get => _b; set => _b = value; }
    public float C => _c;

    public override string ToString()
    {
        return $"{_a}, {_b}, {_c}";
    }
}
class Program
{
    static void Main(string[] args)
    {
        Foo one = new Foo(100, "Philip", 0.75f);

        Console.WriteLine(one);
        //100, Philip, 0.75

        one.B = "Spencer";

        Console.WriteLine(one);
        //100, Spencer, 0.75
        
    }
}

Another feature of the code above is that there is no need to create a .Print() method, if you can take advantage of the built-in .ToString() which gets called automatically during a Console.WriteLine() or any other operations that require a string input from my class.

Finally, I added a read-only property C to demonstrate that this design is possible. You will not be able to write one.C = 0.25f; since the property is read-only.

  •  Tags:  
  • c#
  • Related