✅ 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 🙂
42 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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 againOch, 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.
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
exact same code, only difference is the coffee is changed to pepsi 😛
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"
Sure,
Are you coming from Java?
Never coded in my life before.
Ah, okay just asked cause you have get methods
Is what i want to do clear? or do you need more explaining? :p
Yes, everything is good just you'd take the Liquid name as a parameter
You are correct
void Option(string name);
Omg
yeah, you are correct
and then you'd use the parameter in the first line of the method:
Liquids liquid = new Liquids(name);
Thank you for you time
No problem, I can go and nit pick about a few design issues but besides that all is good
Ohh, do tell !!
I'm all ears.
Liquids
class represents a single drink right?
Like if you do new Liquids("Coffee")
thats a single drinkim 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
correct
Then I'd name the class Liquid not Liquids
Makes it more readable
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
Yes sir ! 😄
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-propertiesreplace getName() with public string Name {get; set;}
same with getPrice (but type is int, not string)
How do i Acces the name get; set; ?
Properties are recommended to use to avoid having to write boilerplate code
You'd do
liquid.Name
Oh
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)
If you don't want it to be settable outside of the class you can remove the
set;
or
private set;
implicitly if
set;
isn't specified it is privateAha ! Thanks guys !
Ah, this is a better link for properties:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
Thank you very much. I have alot the read up on 😛
I'd put curly braces on the if statement, easier to read. Especially if it is followed by an 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
else
or if else
Also you have duplicate code for all possible executions
I'd move that to the 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.