Click here to Skip to main content
15,899,679 members
Home / Discussions / C#
   

C#

 
GeneralRe: Binary serialization Pin
OriginalGriff6-Feb-10 23:59
mveOriginalGriff6-Feb-10 23:59 
GeneralRe: Binary serialization Pin
harold aptroot7-Feb-10 0:13
harold aptroot7-Feb-10 0:13 
GeneralRe: Binary serialization Pin
hain7-Feb-10 4:02
hain7-Feb-10 4:02 
QuestionLabel Edit Bug with Treeview, MDI Container and MDIChildForms Pin
CrazyNinjaMike26-Feb-10 16:27
CrazyNinjaMike26-Feb-10 16:27 
QuestionGetting values to Add up from a datagrid text box Pin
miggs706-Feb-10 13:49
miggs706-Feb-10 13:49 
AnswerRe: Getting values to Add up from a datagrid text box Pin
OriginalGriff6-Feb-10 21:42
mveOriginalGriff6-Feb-10 21:42 
GeneralMessage Removed Pin
7-Feb-10 3:12
miggs707-Feb-10 3:12 
GeneralRe: Getting values to Add up from a datagrid text box [modified] Pin
OriginalGriff7-Feb-10 4:33
mveOriginalGriff7-Feb-10 4:33 
Hi! Sorry for the delay in responding, I had to collect my wife for work, feed her lunch, that kind of thing...


You aren't far off what you want to do, you have just got bogged down in "that works so I'll keep it" syndrome! We all do that, so don't feel bad!

I have modified your code a fair amount, and will explain why as we go through:
Double xVal = 0;

foreach (DataGridViewRow row in d_Endpost.Rows)
    {
    double postSpacing = 0.0;
    double postSpacingMetric = 0.0;
    string userEntry = (string)row.Cells["EPSpacing"].Value;
    if (!string.IsNullOrEmpty(userEntry))
        {
        if (Double.TryParse(userEntry, out postSpacing))
            {
            postSpacingMetric = Imperial2Metric(postSpacing);
            }
        else
            {
            MessageBox.Show("Invalid entry (" + userEntry + ") please enter the post spacing in inches and try again");
            return;
            }
        xVal += postSpacingMetric;
        Console.WriteLine(xVal);
        }
    }
Firstly, I have removed the outer loop - as you have realised, you only need the one and in this case the foreach makes it all a bit more readable. Generally, try to avoid for loops when you can use foreach and don't need to know how many elements you are dealing with.
Next, I specifically get the element value into a string - you don't need to do this, but it both makes it more obvious and ties the error message to it better later. Because we know that a DataGridViewRow.Value property will (in this case) always be a string, we can cast it, with no need for a conversion.
Then, check it isn't empty! If it is, and we don't check, then we will get a null reference exception.
Ok so far, try and parse it. TryParse works like Parse, but instead of throwing an exception if the user types in his name instead of the distance, it returns will a true or false to say the conversion worked. If it fails, tell the user what he did wrong!
It is always worth checking user input - if you don't then a simple mistake can cause your program to fail. Check it, and handle it nicely!
Include the current metric value in the running total.
Display the total so far with Console.WriteLine - this is where you would do whatever you need to with the values as they are being generated.
Why do I not check the first one to start the running total? Why bother? Because I initialise the running total to 0 when I declare it, the first element output will be the first value the user enters. If I don't need to create a special case, I won't bother!

Try running this through the debugger - it is well worth getting used to it, you are going to spend a lot of time looking at your code through debug screens!

Hope that all makes sense!

[edit] included the word "don't" in the phrase "so don't feel bad" - Oops![/edit]
If Barbie is so popular, why do you have to buy her friends?

Eagles may soar, but weasels don't get sucked into jet engines.

If at first you don't succeed, destroy all evidence that you tried.

GeneralRe: Getting values to Add up from a datagrid text box Pin
miggs707-Feb-10 5:04
miggs707-Feb-10 5:04 
GeneralRe: Getting values to Add up from a datagrid text box Pin
OriginalGriff7-Feb-10 5:12
mveOriginalGriff7-Feb-10 5:12 
GeneralRe: Getting values to Add up from a datagrid text box Pin
miggs707-Feb-10 6:10
miggs707-Feb-10 6:10 
GeneralRe: Getting values to Add up from a datagrid text box Pin
OriginalGriff7-Feb-10 8:17
mveOriginalGriff7-Feb-10 8:17 
GeneralMessage Removed Pin
7-Feb-10 11:13
miggs707-Feb-10 11:13 
GeneralRe: Getting values to Add up from a datagrid text box Pin
OriginalGriff7-Feb-10 22:31
mveOriginalGriff7-Feb-10 22:31 
GeneralMessage Removed Pin
8-Feb-10 1:48
miggs708-Feb-10 1:48 
GeneralRe: Getting values to Add up from a datagrid text box Pin
OriginalGriff8-Feb-10 2:05
mveOriginalGriff8-Feb-10 2:05 
Questionoverlapping area of n ractangles Pin
hotthoughtguy6-Feb-10 10:41
hotthoughtguy6-Feb-10 10:41 
Questionchange resource file dynamically Pin
jojoba20106-Feb-10 7:19
jojoba20106-Feb-10 7:19 
AnswerRe: change resource file dynamically Pin
Richard MacCutchan6-Feb-10 9:23
mveRichard MacCutchan6-Feb-10 9:23 
QuestionResource+C# Pin
jojoba20106-Feb-10 5:45
jojoba20106-Feb-10 5:45 
AnswerRe: Resource+C# Pin
Richard MacCutchan6-Feb-10 9:20
mveRichard MacCutchan6-Feb-10 9:20 
Questionproblem with winform visual style in c# Pin
evanxg6-Feb-10 4:53
evanxg6-Feb-10 4:53 
QuestionChange A Property Within The Foreach Loop Pin
BlitzPackage6-Feb-10 3:54
BlitzPackage6-Feb-10 3:54 
AnswerRe: Change A Property Within The Foreach Loop Pin
OriginalGriff6-Feb-10 4:19
mveOriginalGriff6-Feb-10 4:19 
GeneralRe: Change A Property Within The Foreach Loop Pin
harold aptroot6-Feb-10 4:22
harold aptroot6-Feb-10 4:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.