Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to get the Max value between 2 columns and return it to a third column, if they are the same return that values.

Each line in debug shows a value. except for d3 where the value should be placed after it determines the highest value.

Ideas of what I'm doing wrong?

Image of Degug
Image - TinyPic - Free Image Hosting, Photo Sharing & Video Hosting[^]



decimal d1 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[41].Value.ToString()) ? "0" : rows.Cells[41].Value.ToString());
                decimal d2 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[46].Value.ToString()) ? "0" : rows.Cells[46].Value.ToString());
                decimal d3 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[37].Value.ToString()) ? "0" : rows.Cells[37].Value.ToString());
                decimal AL = Math.Max(d1, d2);
                AL = Math.Max(d3,AL);


What I have tried:

the above code should work the return column is blank st start with.
Posted
Updated 16-Nov-18 8:31am
v2
Comments
Eric Lynch 16-Nov-18 14:11pm    
Here, the debugger is your friend. Things I would examine in the debugger...

1) What are the values of your input columns: rows.Cells[41].Value, rows.Cells[46].Value, and rows.Cells[37].Value?
2) What are the results of your conversions: d1, d2, and d3?
3) What is the result of your intermediate Max operation: AL?
4) What is the result of your final Max operation: AL?
5) What is your expected result and how is it different than your actual result?

I guarantee one of these things (probably items 1 or 2) are not what you think.

Folks here can't see your screen, or run the debugger for you, so we're at a disadvantage.

At a minimum, you should improve your question to provide your actual input values (columns 37, 41, and 46), your actual output value (AL), and your expected output value. If any errors occur, you should also include that information.

BTW, instead of decimal.Parse, you may want to consider decimal.TryParse. The latter allows you to catch and handle invalid input more efficiently.
[no name] 16-Nov-18 14:17pm    
"Each line in debug shows a value. except for d3...." What does this means? How can d3 not Show a value?
Member 12349103 16-Nov-18 14:34pm    
I uploaded a link of the Debugger, not sure how to add image
d1=28.444
d2=28.444
d3=0
AL=28.444
[no name] 16-Nov-18 14:47pm    
And what is wrong with the result? AL does hold the value I would expect, because Max(d1, d2) is 28.444 which you store first in AL and therefore Max(d3, AL) will return again 28.444 (and assigned again to AL) because d3 seems to be Zero.... I don't see the Problem...
Eric Lynch 16-Nov-18 16:45pm    
Hopefully, this isn't a double post. My prior comment seems to be missing.

As 0x01AA notes, the correct (mathematical) maximum for these three numbers (0, 28.444, and 28.444) is 28.444. What did you expect?

In an earlier comment, you assert that the debugger does "not show a value" for d3. This is perplexing in two ways. First, this should be impossible for a value type (which defaults to zero). Second, it is inconsistent with the assertion above...that the value of d3 is "0".

Did you perhaps forget to share that an exception occurred (and share the exception text)?

I'll again re-iterate that IsNullOrEmpty is not the only invalid input text. For example, "Mickey Mouse" is not a valid decimal number. This is why I strongly suggest you either add a try/catch block or use decimal.TryParse instead of decimal.Parse.

You don't show us what you do with the eventual value (in AL) but since it's just a variable unless you "add it to a column" it's not going to show.

The first thing you need to do is break out the debugger and look closely at exactly what is happening when you run that code with your data: put a breakpoint on the line
C#
AL = Math.Max(d3,AL);
and look at what is in d1, d2, d3, and AL
That should give you a clue as to what is happening and you can look closer at the relevant data / code once you have that.

Sorry - but we can't do any of that for you!
 
Share this answer
 
Quote:
I want to get the Max value between 2 columns and return it to a third column, if they are the same return that values.

This code calc the max of 3 values and don't try to store the result in another column.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900