1

I have the following code -

int lat = System.Convert.ToInt16(latTextBox1.Text);

This is happening on a changed event.

However my code is breaking on this line saying -

Input string was not in a correct format.

Where latTextBox1.Text = "" which is an empty string. I think this is breaking because it cannot convert an empty string into a null value.

How can I modify my code to account for null values?

Darren
  • 68,902
  • 24
  • 138
  • 144
Ebikeneser
  • 2,582
  • 13
  • 57
  • 111
  • 3
    What do you want `lat` to be if `latTextBox1.Text` is empty? Also not you are calling `Convert.ToIn16` and assigning that to an `int` - you should use `Convert.ToInt32`. – Daniel Kelley Feb 04 '13 at 14:28
  • well in the database it is allowed to be null, so null would be the default value as opposed to Zero, as lat actually stands for latitude, and "0" is a valid latitude value so yes it has to be null. – Ebikeneser Feb 04 '13 at 14:30
  • You can use client side valdiators to eliminate set of issues on client side (surely you need server side validation as well since client validation can be ignored but this requires some skills and rarely happends, anyways use `Int32.TryParse()` to convert string to int safely) – sll Feb 04 '13 at 14:30
  • @sll: from the code above, this is a windows app, not web – Mohammed Swillam Feb 04 '13 at 14:35
  • @Jambo Why are you storing latitude as an `int`? There is no precision needed? http://stackoverflow.com/questions/551894/whats-the-best-way-to-store-co-ordinates-longitude-latitude-from-google-maps – MikeSmithDev Feb 04 '13 at 14:36

9 Answers9

4

OK, based on your comment you could use:

int? lat = string.IsNullOrEmpty(latTextBox1.Text) 
               ? (int?)null : Convert.ToInt32(latTextBox1.Text);

int? is a nullable int.

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
3

Well, Convert.ToInt16 isn't meant to convert an empty string into a null value... indeed it can't, given that the return type is a non-nullable Int16 (aka short).

I suspect you actually want something more like:

int lat;
if (int.TryParse(latTextBox1.Text, out lat))
{
    // Use lat
}
else
{
    // Invalid or empty input. Handle appropriately.
}

If you want to handle empty strings differently to invalid input, you'll need to do that explicitly - although you should also consider whether "just whitespace" is equivalent to "empty" or "invalid".

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

How about a simple:

int? lat = null;
int dummyLat;
if (Int32.TryParse(latTextBox1.Text, out dummyLat)
    lat = dummyLat;

On a side note:
I' never convert strings in the TextChanged event ever! Why? Because it triggers an event upon every keypress! Use some other trigger to initiated the conversion (for example a button).

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

you should first check that the current value is not an empty one before trying to convert it to an integer, kindly check out the following snippet int lat = 0;

  If(! string.IsNullorEmpty(latTextBox1.Text))
    {
     lat = System.Convert.ToInt32(latTextBox1.Text);
    }
// use your lat variable here

Update:

From your comment above, lat may hold the NULL value, so you will have to make it a nullable Int in order to make it hold the value NULL

consider this updated code snippet int? lat = 0;

  If(! string.IsNullorEmpty(latTextBox1.Text))
    {
     lat.value = System.Convert.ToInt32(latTextBox1.Text);
    }
// use your lat variable here by accessing the lat.value property

Note: latiture and longtitude values should be stored in a double datatype in order to preserve the precision.

Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47
  • 2
    Doesn't work as `lat` is declared within the block and thus can't be used outside the block. In addition it will throw a compiler error within the `if` block, because `lat` is declared twice. – Thorsten Dittmar Feb 04 '13 at 14:32
  • `IsNullOrEmtpty` is not a great approach for converting a `string` to an `int`. It does not guarantee the string is convertable. – Quinton Bernhardt Feb 04 '13 at 14:37
  • this is about handling nulls, we can enhance this answer more to take into consideration the wrong values – Mohammed Swillam Feb 04 '13 at 14:41
1

You could use the following method:

Int16.TryParse:

Or you could check if the string is not null or empty before performing the logic:

if (!string.IsNullOrEmpty(latTextBox1.Text)) {
     lat = System.Convert.ToInt16(latTextBox1.Text);
}

http://msdn.microsoft.com/en-us/library/system.int16.tryparse.aspx

Darren
  • 68,902
  • 24
  • 138
  • 144
  • In the second case, will it work if I give a non-interger ? (like some text letters). We get an "Input string was not in a correct format" error – Hari Jul 10 '14 at 11:27
0
int lat = 0;

if (Int32.TryParse(laTextBox1.Text, out lat))
{
    //success, textbox contained an int, now lat has the value of that int
}
else
{
    //failure, textbox did not contain an int
}
H H
  • 263,252
  • 30
  • 330
  • 514
paul
  • 21,653
  • 1
  • 53
  • 54
0

You have to check for the string being set:

int lat = 0;
if (string.IsNullOrEmpty(latTextBox1.Text)
{
    // Your null case here
}
else
{
    lat = System.Convert.ToInt16(latTextBox1.Text);
}

Though:

int lat = 0;
if (!Int32.TryParse(latTextBox1.Text, out lat)
{
    // Error handling here
}

is probably a better approach as it copes with non numeric input.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
0
int i = 0;

if (!Int32.TryParse(latTextBox1.Text, out i)) {

    i = 0; // or default value;
}
Quinton Bernhardt
  • 4,773
  • 19
  • 28
  • 1
    If you plan on having your local variable assigned the value of another function (such as in this case with `Int32.TryParse` and its `out` parameter) then you should _not_ declare it with an initial value as the initial value just gets thrown away. It also enforces your code to make sure it gets assigned a value (via the `out`) before you use it accidentally. – Chris Sinclair Feb 04 '13 at 14:42
-1

One liner:

int lat = System.Convert.ToInt16(latTextBox1.Text.Length == 0 ? 0 : latTextBox1.Text);

Basically works like coalesce, if latTextBox1.Text does not have a value, set it to zero otherwise the value from latTextBox1.Text

whastupduck
  • 1,156
  • 11
  • 25