3

Is there a .Net function that does that. I guess if there isn't i have to make my own method.

The thing is i have a function. It accepts integers. If you pass a 0 integer or a null value it still works. Problem is that the value of an empty textbox is a String.Empty value.

I don't want to use if's. I mean it could but its much nicer if i can just call my function like this

MyFunction(txtTextbox.Text)

But it won't work because it can't convert the string.empty to a integer.

Tomasi
  • 2,517
  • 6
  • 31
  • 43
  • This question is confusing. Is `MyFunction` the same as the function you refer to in the second paragraph? It doesn't seem like it can be since you clearly intended to pass a `string` to it, not "a 0 integer or null". In case you're not aware, you can't just pass a `string` to a function accepting `int` or `int?`. – Marcelo Cantos Feb 22 '10 at 23:45
  • Why not? It accepts integers but if you pass a String it should be implicitly converted anyway. MyFunction("200") - works. – Tomasi Feb 22 '10 at 23:54
  • What if the number represented in the string is too large to fit in an integer? What if the string represents a floating-point number? What if the string is not numerical at all? These are all cases that you should handle, and one clean way to do it is properly transforming the string values into integers _beforehand_. – zneak Feb 23 '10 at 00:09

6 Answers6

4

I guess you need:

if(string.IsNullOrEmpty(value)) value = null;

or

int MyFunction(string value)
 {
      if(string.IsNullOrEmpty(value)) return 0;

      int val = 0;
      int.TryParse(value, out val);
      return val;
 }
Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
  • The latter does not need the extra `if(...) return 0;`. Also, any method taking an `out` parameter must assign a value to it before it returns. So `val` will always be initialized, there is no need to do it again. ( the `=0` part.) – Andras Vass Feb 23 '10 at 00:11
3

What about, um, accepting an integer in your function that should read integers, and use int.Parse or int.TryParse on the string beforehand?

zneak
  • 134,922
  • 42
  • 253
  • 328
0

Just from a different perspective, I assume that your textbox will also need to allow only numeric to be entered. Otherwise just handling null isnt going to be bullet proof against someone entering non numeric. There must either be some maskings, event handler or validation you have created for it. How about create your own NumTextBox that inherit from TextBox with the input restriction behaviours that you already had and either override Text property or create a new property calls Value which looks after all the conversions and return the appropriate value consistently through out your system.

Fadrian Sudaman
  • 6,405
  • 21
  • 29
  • That's a great idea, although is too much work for such a simple thing. – Tomasi Feb 22 '10 at 23:48
  • Seriously, overkill. You can attach a validator to a "normal" textbox much easier than making your own subclass and trying to use that. – Coderer Feb 23 '10 at 00:34
  • Like I said just another perspective. For one used, it is overkill but if the same behaviour is expected everywhere in the application the abstraction and encapsulation will be beneficial – Fadrian Sudaman Feb 23 '10 at 02:24
0

try this

   Sub MyFunction(ByVal Param1Integer as Integer)
     ' Do Something
   End Sub

   Sub MyFunction(ByVal Param1String as String)
     MyFunction(Val(Param1String))
   End Sub

It assumes that an empty string is the same as 0.

Michael Fitzpatrick
  • 682
  • 3
  • 8
  • 18
0

Have you looked into using a NumericUpDown (spin control)? It has a .Value member (always a valid Integer!) instead of having to convert from a String. You can limit the upper and lower values, as well as set a default.

Coderer
  • 25,844
  • 28
  • 99
  • 154
0

I would just use an inline IF statement (VB):

MyFunction(IIf(txtTextBox.Text Is Nothing, "", txtTextBox.Text))
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Losbear
  • 3,255
  • 1
  • 32
  • 28