0

Why second method receives 0 value?

The problem is that I have two methods. 1. value from first one(double) is returned (return valueX;) 2. now I want to pass this again into another method(string) 3. to be then checked and return appropriate message depending on valueX. Problem is that method2(string) receives valueX(from method 1 - double) as 0 and not the actual value. Value of method 1 is printed out correctly. Link: https://code.sololearn.com/ctx0pvhxH3zw/?ref=app

4th May 2018, 11:44 AM
Karol Michalik
Karol Michalik - avatar
9 Réponses
+ 1
I add some debug line. The problem is not in the variables. Although using variables in this way is not the most elegant way. using pass by ref would be better. https://code.sololearn.com/c6XnVxQ8TSLg return String.Format("{0}'s division is {1} \n roll number is {2}\n grades \n{3} \n{4} \n{5} and total sum {6} which equals {7}%",Name, whichDivision(), RollNo, MPhysics, MChem, MCompApp, sumOfGrades(),pretzel()); whichDivision is executed before pretzel is executed So Percent is still 0 when whichDivision is executed Do not mix method-execution and string.format. First Calculate the variables then make your output.
5th May 2018, 8:36 PM
sneeze
sneeze - avatar
+ 1
Silly me. Value for variable is 0. It changes during method evaluation. And value is actually not returned. So what I mean is that at the beginning Percent =0 then during evaluation of pretzel() something must be returned. Either way if we pass not oretzel() but Percent to to String method we will still receive 0. To avoid this I should have used reference type I guess
4th May 2018, 11:55 AM
Karol Michalik
Karol Michalik - avatar
+ 1
Name of method with incorrect output is whichDivision () Input Var Percent with value calculated by method pretzel () So instead of 80%->failed should br 80%->first and so on. Pretzel() calculates output correctly. If you change the data it will still provide good answer. I have tried this on ten new objects with different values..
5th May 2018, 7:39 AM
Karol Michalik
Karol Michalik - avatar
+ 1
Well Done In this code, I made an object for every user input using a while loop. https://code.sololearn.com/cJCbxF0UC1qb In the playground I used this test-script James, 784, 70, 80, 90 Harrison Solo, 100, 100, 100, 100 Janush Smith, 130, 10, 10, 10 stop Where "stop" is the trigger to stop executing the program
6th May 2018, 9:47 PM
sneeze
sneeze - avatar
0
Did you solve the problem or can you explain a bit more. You description is very vague due using method1 and method2 instead of real names. My current result is that james failed and he scored 80%
4th May 2018, 8:19 PM
sneeze
sneeze - avatar
0
Mine also. I am już guessing that only solution would be to use reference type in methods with which I am not familiar. Well I have only opened the lecture here at SoloLearn. I was thinking about overload but overload is for input not output of method I tried to also pass the method to method (still 0).
5th May 2018, 7:26 AM
Karol Michalik
Karol Michalik - avatar
0
Passing by ref is probably gone a help you. What variabele do you want to show / change . What is the name op the method that does not have the correct input. is 80% incorrect ? what should be the result ?
5th May 2018, 7:32 AM
sneeze
sneeze - avatar
0
Thank you. But still if I evaluate your code what I receive in division is failed"... Oh now I see. So I should output data straight away instead of drag the data to the very end and then try to output this (and maybe it will be fine) And no method evaluation in strings. Ok. Yeah I know that reference would be much more clean and elegant but I was searching for universal way. And just series of strings is not universal at all. BTW is there a way to create object everytime user inputs something? Like here. User inputs grades, student roll number and name and then inputs another piece of data (another student)?
5th May 2018, 11:04 PM
Karol Michalik
Karol Michalik - avatar
0
https://code.sololearn.com/c2gfBpZbTBZI/?ref=app OK now it's fine. Like you said first I have executed my methods and then listed values. Works perfectly!
6th May 2018, 7:35 AM
Karol Michalik
Karol Michalik - avatar