none
Converting from null to 0 RRS feed

  • Question

  • I'm trying to convert from no value being input to a zero value being taken, my attempt looks like this:

    if (openingWidth == null) { openingWidth = Convert.ToInt32(0); }

    It compiles but for some reason it doesn't seem to pass in the zero. I presume no input is considered a null input?

    Any ideas welcome,

    thanks.

    Saturday, February 9, 2019 5:39 PM

Answers

  •  when i input the condition it puts a green line under '(openingWidth == null)' giving me more cause to think it was invalid. 

    You will get an underline because openingWidth is an int which is not a
    nullable type. So it is impossible for openingWidth ever to be equal to null
    and therefore the conditional test must always be false.

    If it were nullable:

    int? openingWidth

    then the conditional test - if (openingWidth == null) - would be a valid check.

    - Wayne

    • Marked as answer by matty1248 Saturday, February 9, 2019 11:59 PM
    Saturday, February 9, 2019 11:51 PM
  •  I'd best wait till the morning to give that a go, hopefully it will suit.

    >if (!int.TryParse(s, out n)) s = n.ToString();

    Since we know that TryParse will set the out parameter to 0 when the conversion
    fails we can also simply do this:

    if (!int.TryParse(s, out n)) s = "0";

    Note that setting the string to zero based on the result of TryParse will
    mean that *any* invalid input will be changed to zero, not just empty strings.
    If the user enters "123.45" or "$60" or "Uncle Wiggly" they will be replaced by "0".

    If you want to convert empty strings to zero, and to leave all other invalid
    string entries as entered, then test for that specific input.
    You can do that before or after TryParse is executed.

    if (s == "") s = "0";
    int.TryParse(s, out n);
    

    - Wayne


    • Marked as answer by matty1248 Sunday, February 10, 2019 10:24 AM
    Sunday, February 10, 2019 7:50 AM

All replies

  • I'm trying to convert from no value being input to a zero value being taken, my attempt looks like this:

    if (openingWidth == null) { openingWidth = Convert.ToInt32(0); }

    It compiles but for some reason it doesn't seem to pass in the zero. I presume no input is considered a null input?


    What type is openingWidth?

    The default value may not be null as you assume. It could be zero or an empty
    string, etc. Whatever type it is, have you checked its initial value using
    the debugger? Have you used the debugger to see if the program ever enters
    the code block for the if statement? Or are you just making a lot of assumptions?

    Why are you using Convert.ToInt32 with an integer argument? When you do that
    no actual conversion occurs, it just returns the int that was passed as an 
    argument. So it is no different then just doing

    openingWidth = 0;

    You use Convert.ToInt32 to convert a string such as

    openingWidth = Convert.ToInt32("0"); 

    In any event these code examples all set openingWidth to zero:

    Nullable<int> openingWidth = null;
    
    openingWidth = Convert.ToInt32(0);
    
    openingWidth = Convert.ToInt32("0");
    
    openingWidth = 0;
    
    

    So I think you need to provide more information on exactly what you are doing 
    and how you are checking the result.

    - Wayne

    Saturday, February 9, 2019 10:33 PM
  • I see, well my source is a textbox but i've parsed it to an integer already, i didn't realise there were so many conditions. This validation reads straight from the textbox:

    !int.TryParse(InputOpening1Width.Text, out int openingWidth)

    then i call that as above:

    if (openingWidth == null) { openingWidth = Convert.ToInt32(0); }

    I guess i should have asked on the UWP forums, i just didn't realise there were so many dependancies, it's not very active over on UWP.

    Thanks for your response.

    Regards,

    Matthew

    Saturday, February 9, 2019 10:53 PM
  •  my source is a textbox but i've parsed it to an integer already, iThis validation reads straight from the textbox:

    !int.TryParse(InputOpening1Width.Text, out int openingWidth)

    then i call that as above:

    if (openingWidth == null) { openingWidth = Convert.ToInt32(0); }


    If TryParse fails it will set the out parameter (openingWidth) to zero.

    Int.TryParse will fail if the string parameter is null or Empty, is not of the 
    correct format, or represents a number less than MinValue or greater than 
    MaxValue. 

    The result contains the 32-bit signed integer value equivalent of the number 
    contained in the string. if the conversion succeeded, or zero if the conversion
    failed. 

    So your conditional test for null will always fail. But openingWidth will
    already be zero if the input is empty.

    - Wayne


    • Edited by WayneAKing Saturday, February 9, 2019 11:38 PM
    Saturday, February 9, 2019 11:30 PM
  • Thanks Wayne, that's what I thought, and when i input the condition it puts a green line under '(openingWidth == null)' giving me more cause to think it was invalid. The thing is though, when i don't add the zero's it gives me the wrong/a different output:

    I'm a bit baffled as to why, there are no other conditions on this textbox.

    Cheers,

    Matthew

    Saturday, February 9, 2019 11:38 PM
  •  when i input the condition it puts a green line under '(openingWidth == null)' giving me more cause to think it was invalid. 

    You will get an underline because openingWidth is an int which is not a
    nullable type. So it is impossible for openingWidth ever to be equal to null
    and therefore the conditional test must always be false.

    If it were nullable:

    int? openingWidth

    then the conditional test - if (openingWidth == null) - would be a valid check.

    - Wayne

    • Marked as answer by matty1248 Saturday, February 9, 2019 11:59 PM
    Saturday, February 9, 2019 11:51 PM
  • Thanks for your help, not sure why it's not outputting as I expect but that's an option ruled out i guess. It's not critical, if all else fails i'll just fall back to '0' placeholders instead of the placeholder text i was going to use.

    Cheers,

    Matthew

    Sunday, February 10, 2019 12:03 AM
  • Strange though, my debugger says they are all empty strings after validation:

    Sunday, February 10, 2019 12:25 AM
  • Hey Matthew,

    You haven't actually executed that line yet … the breakpoint is on it, but you should debug step to the next line. But that's not really your problem or what you just wondered about: if the user has not actually entered anything into the TextBoxes, their .Text property will still be an empty string.

    Let me ask you this … are you trying to initialize the .Text in the TextBoxes to be zero to being with?


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, February 10, 2019 12:57 AM
    Moderator
  • Strange though, my debugger says they are all empty strings after validation:

    I'm not sure what you're saying there. By "validation" are you referring to the
    TryParse operations? If so, are the empty strings to which you are referring
    the strings that TryParse is converting to integers? If so, why should it be
    otherwise? TryParse doesn't change the *string* that is passed to it. If it
    is an empty string before TryParse it will still be an empty string after.

    - Wayne

    Sunday, February 10, 2019 1:33 AM
  • Hi Bonnie, I didn't get into the habit of using the debug tools as much as i should :/, I should really get to grips with that, it's becoming an issue the longer my codes get. Is this one correct:

    It still seems to give an empty string, the other values have been input on my main inputs pane as defaults to speed up testing:

    Really i just want the user to have the option to not be required to input openings if they don't have any. I want to have placeholder text in all fields. I want the empty boxes to initialise to zero, when left blank, as you say. 


    • Edited by matty1248 Sunday, February 10, 2019 1:40 AM
    Sunday, February 10, 2019 1:39 AM
  • Further to my last comment, if you wanted to change empty strings to "0" there
    are various ways. Including converting the int back into a string:

    string s = "";
    int n = -1;
    if(!int.TryParse(s, out n)) s = n.ToString();
    

    - Wayne

    Sunday, February 10, 2019 1:48 AM
  • That's what i was meaning by validation, yes, perhaps wrongly. I had kind of presumed that was the case for an empty string and so i'd used the statements 'if (openingWidth == null) { openingWidth = Convert.ToInt32("0"); }' etc to try to overcome that, I just can't find the way to convert those values to zero's where no value is given.

    Cheers,

    Matthew

    Sunday, February 10, 2019 1:49 AM
  • Thanks Wayne, I'd best wait till the morning to give that a go, hopefully it will suit.

    Cheers :), 

    Matthew

    Sunday, February 10, 2019 1:52 AM
  •  I'd best wait till the morning to give that a go, hopefully it will suit.

    >if (!int.TryParse(s, out n)) s = n.ToString();

    Since we know that TryParse will set the out parameter to 0 when the conversion
    fails we can also simply do this:

    if (!int.TryParse(s, out n)) s = "0";

    Note that setting the string to zero based on the result of TryParse will
    mean that *any* invalid input will be changed to zero, not just empty strings.
    If the user enters "123.45" or "$60" or "Uncle Wiggly" they will be replaced by "0".

    If you want to convert empty strings to zero, and to leave all other invalid
    string entries as entered, then test for that specific input.
    You can do that before or after TryParse is executed.

    if (s == "") s = "0";
    int.TryParse(s, out n);
    

    - Wayne


    • Marked as answer by matty1248 Sunday, February 10, 2019 10:24 AM
    Sunday, February 10, 2019 7:50 AM
  • Brilliant! Problem solved. These validation and parsing functions seem to be a real weak spot for me, admittedly of many. I think i'll try to finish this app then hone in a lot more on theory. At times it seems like i'm getting a bit ahead of myself with this app, but I have terrible cognition and I find it really valuable just building recognition for the terms, and it's a lot more motivating trying to build something and learning what functions are valuable for. When i was doing the basic theory things like looping and validation seemed so abstract, then when i get building i realise that they're fundamental. Really appreciate your help,

    Regards,

    Matthew

    Sunday, February 10, 2019 10:24 AM