C
C#2y ago
Kekke

✅ Change a variable in method.

I'm having a brainfart moment. You see I noticed in my programm that i have 7 methods that do the same, but just with one different variable, I only want to have 1 method that does the same thing , but when i call it i change the variable myself, heres the code: https://imgur.com/EO43Unh Im noob 🙂
Imgur
42 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Bin
Bin2y ago
not sure what your intention here, dont understand your question completely, but anyway you could store the result of coffee.getPrice() call to a variable, e.g double coffeePrice = coffee.getPrice() then reuse the same variable of coffeePrice whenever you need to get the price again
Kekke
KekkeOP2y ago
Och, i'll try to explain 😛 You see all those wich i underlined in red. whenever i call the method i want to change those. So instead of coffe it says cola or pepsi instead, because i have those different items, in the liquid class. Just because im breaking the DRY rule a whole lot here.
Bin
Bin2y ago
if you want to just change the variable name, you can assign it to another variable. Beware that if it is a class, it will reflect change made from any variable across different variable where you assign them to different variables
Liquid coffee = new Liquid()
Liquids pepsi = coffee
Liquids fanta = pepsi
Liquid coffee = new Liquid()
Liquids pepsi = coffee
Liquids fanta = pepsi
Kekke
KekkeOP2y ago
exact same code, only difference is the coffee is changed to pepsi 😛
Pendramon
Pendramon2y ago
Pass the name as a parameter to the method? Although I don't know what name you'd give the method the variable name would then just be "liquid"
Kekke
KekkeOP2y ago
Sure,
Pendramon
Pendramon2y ago
Are you coming from Java?
Kekke
KekkeOP2y ago
Never coded in my life before.
Pendramon
Pendramon2y ago
Ah, okay just asked cause you have get methods
Kekke
KekkeOP2y ago
Is what i want to do clear? or do you need more explaining? :p
Pendramon
Pendramon2y ago
Yes, everything is good just you'd take the Liquid name as a parameter
Kekke
KekkeOP2y ago
You are correct
Pendramon
Pendramon2y ago
void Option(string name);
Kekke
KekkeOP2y ago
Omg yeah, you are correct
Pendramon
Pendramon2y ago
and then you'd use the parameter in the first line of the method: Liquids liquid = new Liquids(name);
Kekke
KekkeOP2y ago
Thank you for you time
Kekke
KekkeOP2y ago
Pendramon
Pendramon2y ago
No problem, I can go and nit pick about a few design issues but besides that all is good
Kekke
KekkeOP2y ago
Ohh, do tell !! I'm all ears.
Pendramon
Pendramon2y ago
Liquids class represents a single drink right? Like if you do new Liquids("Coffee") thats a single drink
Bin
Bin2y ago
im a bit late i was disconnedted. did you mean u want to rename the variables quickly? if so, Press F2 if you are using visual studio code if you want to rename variables
Kekke
KekkeOP2y ago
correct
Kekke
KekkeOP2y ago
Pendramon
Pendramon2y ago
Then I'd name the class Liquid not Liquids Makes it more readable
Bin
Bin2y ago
liquids is plural, rename it to liquid if it is single also rename parameter name i to price for readability, since it is used to pass in into the price of the liquid
Kekke
KekkeOP2y ago
Yes sir ! 😄
Pendramon
Pendramon2y ago
C# has properties which basically encapsulate a field which makes the getName and getPrice methods not really needed. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties
Bin
Bin2y ago
replace getName() with public string Name {get; set;} same with getPrice (but type is int, not string)
Kekke
KekkeOP2y ago
How do i Acces the name get; set; ?
Pendramon
Pendramon2y ago
Properties are recommended to use to avoid having to write boilerplate code You'd do liquid.Name
Kekke
KekkeOP2y ago
Oh
Bin
Bin2y ago
you don't you just reference it, e.g string LiquidName = liquid.name to get the name of the liquid to set it: liquid.Price = (insert new price here)
Pendramon
Pendramon2y ago
If you don't want it to be settable outside of the class you can remove the set;
Bin
Bin2y ago
or private set;
Pendramon
Pendramon2y ago
implicitly if set; isn't specified it is private
Kekke
KekkeOP2y ago
Aha ! Thanks guys !
Kekke
KekkeOP2y ago
Thank you very much. I have alot the read up on 😛
Pendramon
Pendramon2y ago
I'd put curly braces on the if statement, easier to read. Especially if it is followed by an else or if else Also you have duplicate code for all possible executions
Console.ReadKey();
ShowMeny();
Console.ReadKey();
ShowMeny();
I'd move that to the end of the method outside of the if/else Oh thats not the end of the method, just after the if/else inside the else
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server