Home > Blockchain >  Having trouble with this method in c# wont clear variables
Having trouble with this method in c# wont clear variables

Time:04-22

I am trying to save current variables into list and then clear variables

    static void SaveInvoice(string name, string brand, double tireSize, double metalPrice, double donation, List<Customer> customerList)
    {
        //create object
        Customer newCustomer = new Customer(name, brand, tireSize, metalPrice, donation);
        customerList.Add(newCustomer);
        Clear();
    }

    static void Clear()
    {

        string name = "No name";
        string brand = "No name";
        double metalPrice = 0;
        double tireSize = 0;
        double donation = 0;

    }

After storing to list display here

static void DisplayAllInvoices(List<Customer> customerList)
    {
        Console.WriteLine("\nThe Right Speed Shop");
        Console.WriteLine("*************************");
        Console.WriteLine("\n{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", "Name", "Brand", "Tire", "Metal Price", "Donation");
        Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", "********", "********", "********", "************", "********");

        for (int i = 0; i < customerList.Count; i  )
        {
            Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", customerList[i].Name, customerList[i].Brand, customerList[i].TireSize, customerList[i].MetalPrice, customerList[i].Donation);
        }



    }

CodePudding user response:

In the Clear() method you actually create new variables that are scoped to this method itself. In other words, you just create five new variables local to the Clear() method.

Also, you cannot change the value of value types, like double inside a function (you can, but it will not propagate back in the enclosing scope).
You can do this for reference types, like a string but it's not a good practice (in most cases). Why do you need to clear these values? In my view you don't need the Clear() method. You just call the SaveInvoice with new values any time you want and it will add a customer to the list (given that the customerList is defined).

CodePudding user response:

Your code have some troubles. Read the comments:

static void SaveInvoice(string name, string brand, double tireSize, double metalPrice, double donation, List<Customer> customerList)
{
    // You create newCustomer in this static method and store in the list. 
    // After that, you never use newCustomer. You don't need clear
    Customer newCustomer = new Customer(name, brand, tireSize, metalPrice, donation);
    customerList.Add(newCustomer);

    // To clear newCustomer, you need use newCustomer Clear method, not a static method
    newCustomer.Clear();
}

// You must remove static to work with your instance
void Clear()
{
    // Then, you can use "this" properties
    this.name = "No name";
    this.brand = "No name";
    this.metalPrice = 0;
    this.tireSize = 0;
    this.donation = 0;
}

But be carefull. If you invoke Clear, you clear the newCustomer properties and this object is stored in your list. So your List item will have that properties cleared. I think you only need, the new and Add, without use Clear method.

  • Related