+ 2

Can someone tell me how to extract a value out of an if statement in c#

Value if statement

9th Nov 2020, 5:19 PM
Fred
Fred - avatar
28 Antworten
+ 3
This is my interpretation of your code: https://code.sololearn.com/cEUk1nxJ1S1t/?ref=app
9th Nov 2020, 9:05 PM
JaScript
JaScript - avatar
+ 7
Fred Kwame Ayanful DOH... I missed all the messages that clarified your original code sample. So my last message was way off base. Based on your code sample, you could do the following: var v = (a > 10) ? 2d + ((a - 10) * 0.25) : 2d;
9th Nov 2020, 5:55 PM
David Carroll
David Carroll - avatar
+ 5
I could help clean the code up if I could understand what it was doing. I would have to rename all the variables to something I could follow since I'm unfamiliar with what appears to be German with a touch of Dutch. Or maybe it's all German or all Dutch. It's even tougher to adjust my eyes with camel case variable names. 😉 I can only imagine the challenge for those who don't speak or read English in reviewing the majority of the code written in English.
9th Nov 2020, 7:15 PM
David Carroll
David Carroll - avatar
+ 5
Fred Kwame Ayanful Hmmm... 🤔 Technically, the ternary expression would be better as the variables are declared and initialized with conditional assignments at the same time, thereby eliminating the need to initialize with a default value, later overwrite that value based on a condition, then read that value afterwards for the first time. The ternary expression also eliminates the need to read through all the code only to discover that meaningful assignment doesn't actually occur until the if blocks. Currently, there is a bit of code between the declaration and the conditional blocks where other changes could have occurred. This is what makes readability tougher. I would have implemented the changes if you hadn't indicated that you "can see it clearly now." Let me know if it's still unclear or if I misunderstood your actual question. 😉👌
11th Nov 2020, 6:46 PM
David Carroll
David Carroll - avatar
+ 3
Now with your code the situation is completely different. You have to juggle skillfully with the logic (if / else) and the order of it. The variables contained in the last assignment could also be given a value at their declaration, for example 0. This should prevent that at the end of the calculation one of them remains without a value.
9th Nov 2020, 6:42 PM
JaScript
JaScript - avatar
+ 3
Fred Kwame Ayanful Hey have a treat for you. 😜 I used the code from JaScript as a starting point and decided to apply a series refactored changes with notes in each version. You can compare the changes starting from v2.0 through v2.4. Each Refactored version will introduce improvements that many beginners can study and learn from. I could have gone further from there, but I thought this was good enough for now. 😉 I hope you find v2.4 to be helpful. -------- History of Refactored Versions: ---- Refactored v2.4: (Latest) https://code.sololearn.com/cU2Gqh70Cn7K/?ref=app Refactored v2.3: https://code.sololearn.com/cMz46Rq4sdzh/?ref=app Refactored v2.2: https://code.sololearn.com/c7Q8nzGuFWjV/?ref=app Refactored v2.1: https://code.sololearn.com/cNz3C9HZ4bST/?ref=app Refactored v2.0: https://code.sololearn.com/cQOb48qoebTv/?ref=app
12th Nov 2020, 5:12 AM
David Carroll
David Carroll - avatar
+ 3
Fred Kwame Ayanful Dude... Just so you know... I don't work for SoloLearn. My help wasn't because of your membership. I'm just a dude, helping another dude. 😉👌
12th Nov 2020, 7:29 AM
David Carroll
David Carroll - avatar
+ 2
You should it declare outside such as: double vervoerKosten; if (afstandWerkPlaats <= 10) vervoerKosten = 2;
9th Nov 2020, 5:38 PM
JaScript
JaScript - avatar
+ 2
Fred Kwame Ayanful I don't think GrafGrihula understood what you were asking nor did he see the selected answer. His code is similar to your original.
11th Nov 2020, 7:38 AM
David Carroll
David Carroll - avatar
+ 2
Fred Kwame Ayanful Ternary operator is short hand for the if / else based assignments. The format follows: ---- var x = condition ? valueA //if condition is true : valueB; //else use this value -- Is short hand for this code: ---- int x; if(condition) { x = valueA; } else { x = valueB; } -- For example... this code: ---- var x = some_flag ? 20 : 10; -- Is short hand for this code: ---- int x; if(some_flag) { x = 20; } else { x = 10; } -- Other simple examples could be: ---- If age is 18 or greater, then assign "legal", else "too young" -- (age >= 18) ? "legal age" : "too young"; ---- If total is greater than 0, then assign total as a string, else "No funds" -- (total > 0) ?
quot;{total}" : "No funds"; ---- if condition == true ( use value after ? ) else ( use value after : ) Hopefully, this helps clear it up. 😉 UPDATE: This article might also help: https://kodify.net/csharp/variables/set-variable-conditionally/#option-1-set-a-variables-value-with-an-if-statement
11th Nov 2020, 10:24 PM
David Carroll
David Carroll - avatar
+ 2
David Carroll ah ok, but anyway, thank you for your time and patience 👏
12th Nov 2020, 7:34 AM
Fred
Fred - avatar
+ 1
Hi didn't get what you mean ,can you describe in detail what you want to do?
9th Nov 2020, 5:22 PM
Abhay
Abhay - avatar
+ 1
David Carroll Thanks I will give it a try tomorrow and i will keep you informed and i will also tag the discussion as c#
9th Nov 2020, 5:52 PM
Fred
Fred - avatar
+ 1
Nice, i can see it clearly now, i will give it a try and post the final source code when i´m finished :-)
9th Nov 2020, 5:59 PM
Fred
Fred - avatar
+ 1
It´s in dutch, because it´s flemish school assignment, i will translate and add comments to it starting of tomorrow.
9th Nov 2020, 7:18 PM
Fred
Fred - avatar
+ 1
Thanks JaScript i will run it on my computer tomorrow to test it and ask you my questions( i have a lot 😉). Fine evening to you
9th Nov 2020, 9:15 PM
Fred
Fred - avatar
+ 1
You welcome Fred Kwame Ayanful
9th Nov 2020, 10:04 PM
JaScript
JaScript - avatar
+ 1
David Carroll indeed, i thought that instruction "return" will return the value of a variable outside of the if statement
11th Nov 2020, 8:56 AM
Fred
Fred - avatar
0
Hallo Abhay, i want to use the value of an if condition outside an if statements so that I can declare it to an variable. if (afstandWerkPlaats <= 10) vervoerKosten = 2;
9th Nov 2020, 5:31 PM
Fred
Fred - avatar
0
I would like the value (2) of variable "vervoerKosten" be usable outside of the if statement
9th Nov 2020, 5:33 PM
Fred
Fred - avatar