views:

858

answers:

6

I need to maintain a legacy VB.Net web application. Data typing is, I would say, inconsistent. Especially, there are data that is stored sometimes as integers and sometimes as strings and I have to parse strings to integers reliably. If parsing goes wrong it should always return 0.

The problem is, I can't use any of the .NET/VB.Net parsing functions for this, but have to rely on a self-made function.

The question is, could I use a one-line standard framework call to do every String-to-Integer parsing task?

Let's say there is a string that can be null, empty, contain an integer representation like "10" or contain something else.

I have tried these with an empty input string "":

CType(string, Integer) -> Conversion from string "" to type 'Integer' is not valid.
Convert.ToInt32(string) -> Input string was not in a correct format.
Integer.Parse(string) -> Input string was not in a correct format.
CInt(string) -> Conversion from string "" to type 'Integer' is not valid.
Val(string) -> Success!

But even Val can fail. The surefire way is to call a self-made function:

   Public Function ToInteger(ByVal s As String) As Integer

        s = Trim(s)
        Dim i As Integer

        Try
            i = Val(s)
        Catch ex As Exception
            i = 0
        End Try

        Return i

    End Function

I think this sucks. This is bad because:

  • I'm trying to parse strings to integers! This is not rocket science, even if semantics are involved
  • self-made standards do not stick very well, somewhere in the code you will always find broken standard framework solutions

As a result there are unnecessary bugs in the software. And I accuse the standard framework for this. Unless, of course a better solution is found :)


Thanks for all the answers. Int32.TryParse is perfect here.

But if you have to cast the input to a string first, the cast can fail. Like when reading from a database object with a possible DBNull value.

A: 

Check out IsNumeric()

Stefan
+11  A: 

Use Int32.TryParse and ignore the return value - just use the value in the out parameter. You'll have to do the trimming yourself though.

Jon Skeet
Int32.TryParse is perfect here, thanks-mika-
mika
+5  A: 

You didn't clarify why you couldn't use ".NET/VB.Net parsing functions" so I'll propose this:-

Public Function ToInteger(ByVal s As String) As Integer
    Dim i as Integer
    Integer.TryParse(s, i)
    Return i
End Function
AnthonyWJones
+1  A: 

Have you tried the Int32.TryParse() method?

Claus Hemberg Jørgensen
+1  A: 
lc
The reason you don't need to initialize it isn't because the default is 0, but because it's an out parameter - its previous value is irrelevant.
Jon Skeet
True - I got hung up on the question's stipulating it should return 0 if parsing goes wrong, thinking the behavior was undefined if it fails (out = it has to set /something/ but not necessarily zero). Of course, checking the documentation for TryParse indeed reveals that i would be set to zero.
lc
A: 

If you are doing lots of string parsing and are unsure what might be in the string you could try using Regular expressions. 2 or 3 well chosen regular expressions should dump out whatever you are looking for.

A tool like regexbuddy is invaluable in a situation like this. It would allow you to design regex for you're strings in a gui environment where you could test them on your various alternatives.

For example the following will match a number anywhere in a string and match multiple numbers seperated by spaces.

(\d*)

Regex are sometime thought to be a bit scary but they don't need to be.

Toby Allen