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.

3
  • 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?. Commented Feb 22, 2010 at 23:45
  • Why not? It accepts integers but if you pass a String it should be implicitly converted anyway. MyFunction("200") - works. Commented Feb 22, 2010 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. Commented Feb 23, 2010 at 0:09

6 Answers 6

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.)
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?

Comments

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.

3 Comments

That's a great idea, although is too much work for such a simple thing.
Seriously, overkill. You can attach a validator to a "normal" textbox much easier than making your own subclass and trying to use that.
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
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.

Comments

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.

Comments

0

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

MyFunction(IIf(txtTextBox.Text Is Nothing, "", txtTextBox.Text))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.