0

Here's a puzzle, I am receiving a number in the form of a string. I need to check this input by making sure it is a valid unsigned 32 bit number (negatives should be rejected). The catch is it's for a '0 warnings' project, so this doesn't work:

    try
    {
        uint input = Convert.ToUInt32(textBoxSN.Text.ToString());
    }
    catch(Exception ex)
    {
        //error handling
    }

because I get a warning "the variable 'ex' was declared but never used." And I have no useful way to use the variable. If write a line using 'ex' that has no effect I'll then get a warning for THAT.

Furthermore, the way my program is structured, I don't want to do anything with the variable 'input' in this function, just validate. So I also get the warning that 'input' was declared but never used. Any ideas? Not super important just an interesting problem in C#.

Jedediah
  • 133
  • 1
  • 11

2 Answers2

4

Unless I am missing something here, just use UInt32.TryParse:

UInt32 ui;

if (UInt32.TryParse("100", out ui))
    Console.WriteLine("100: Valid");
else
    Console.WriteLine("100: Not Valid");

if (UInt32.TryParse("-100", out ui))
    Console.WriteLine("-100: Valid");
else
    Console.WriteLine("-100: Not Valid");

Output:

100: Valid
-100: Not Valid

No compiler warnings shown.

In your case you would use it like so:

UInt32 input;

if (UInt32.TryParse(textBoxSN.Text, out input))
{
    // Is valid
}
else
{
    // Is not valid
}

EDIT

Following comment from OP about not actually wanting to consume the output of the TryParse statement:

if (UInt32.TryParse("100", out _))
    ...

This will discard the result of the output parameter and remove any compiler warning if you are not using a variable to store the output.

In your case if you wanted to handle non-valid values only, you could do something like:

if (!UInt32.TryParse(textBoxSN.Text, out _))
{
    // Is not valid
}
Martin
  • 16,093
  • 1
  • 29
  • 48
  • If I'm not mistaken you got no compiler warnings because you used 'input' to print to the console. I don't use 'input' in this function so I get a warning on it. – Jedediah May 23 '19 at 21:05
  • @Jedediah Please see the update using `out _` which discards the result stored in the output parameter. – Martin May 23 '19 at 21:08
  • @Jedediah please reconsider “I don't use 'input' in this function”; at least use it in the error message. “1e+3 is not a valid integer” is a much more useful message than “Not valid”, especially if there are other things that can also make your input not valid. – Dour High Arch May 23 '19 at 21:35
0

You could also just write a simple method that takes a string and returns a bool that reflects whether or not the input was a valid uint. Note we use uint.TryParse here, which returns a bool indicating success if the string argument was successfully parsed to a uint, and it sets the out parameter to the parsed value on success:

public static bool IsUint32(string input)
{
    uint result;
    return uint.TryParse(input, out result);
}

Then you can use it in your code:

if (IsUint32(textBoxSN.Text))
{
    // Valid input was entered
}

In general, it's always better to avoid using exception handling (try/catch) for validation, because it's more expensive and leads to messy code.

Rufus L
  • 36,127
  • 5
  • 30
  • 43