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
binstarjs
binstarjs2y 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
Kekke2y 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.
binstarjs
binstarjs2y 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
Kekke2y 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
Kekke2y ago
Sure,
pendramon
pendramon2y ago
Are you coming from Java?
Kekke
Kekke2y ago
Never coded in my life before.
pendramon
pendramon2y ago
Ah, okay just asked cause you have get methods
Kekke
Kekke2y 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
Kekke2y ago
You are correct
pendramon
pendramon2y ago
void Option(string name);
Kekke
Kekke2y 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
Kekke2y ago
Thank you for you time
Kekke
Kekke2y ago
pendramon
pendramon2y ago
No problem, I can go and nit pick about a few design issues but besides that all is good
Kekke
Kekke2y 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
binstarjs
binstarjs2y 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
Kekke2y ago
correct
Kekke
Kekke2y ago
pendramon
pendramon2y ago
Then I'd name the class Liquid not Liquids Makes it more readable
binstarjs
binstarjs2y 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
Kekke2y 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
binstarjs
binstarjs2y ago
replace getName() with public string Name {get; set;} same with getPrice (but type is int, not string)
Kekke
Kekke2y 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
Kekke2y ago
Oh
binstarjs
binstarjs2y 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;
binstarjs
binstarjs2y ago
or private set;
pendramon
pendramon2y ago
implicitly if set; isn't specified it is private
Kekke
Kekke2y ago
Aha ! Thanks guys !
Kekke
Kekke2y 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
More Posts