Alternative to Exceptions

G

Guest

Hello. I have an exception performance issue. What I am doing is trying to
cast a substring into an integer with CINT(string.substring(x,y)). And if it
doesn't work, I am catching the exception and then entering the substring
into a string variable. Here is my code.

Try
record9row.Item(y) = CInt(lineOfText.Substring(x, record9Sizes(y)))
Catch ex As Exception
record9row.Item(y) = lineOfText.Substring(x, record9Sizes(y))
End Try

The problem is I am looping through this over 500,000 times (populating a
database) and it takes a long time. Is there an alternative to exceptions
that could be used to check if the value in the substring can be cast into an
integer value? This is just taking too long.

Thanks in advance.

Matt
 
P

Patrice

See Int32.TryParse (2.0) or IsNumeric...

Generally exceptions should be for exceptional code i.e. the idea is to test
first that it would succeed rather than giving it a try and raising an
exception as part of the normal control flow (even if not using tryparse you
could have checked for example that the string contains nothing else than 0
to 9 characters)...

Also as a side note I don't really see the difference here (it seems like
you are populating the same column anyway ?)
 
G

Guest

Thanks for the response Patrice. TryParse and IsNumeric are the two things I
was looking for.

I'm not claiming to have exceptional code, either. It was just the only
thing I could come up with at the time (I am new to .NET and am not familiar
with all its features yet).

I am populating the same column, but I am entering the column into a
database from a flat file from a mainframe. The mainframe uses spaces rather
than zeros in some areas and VB chokes when I tell it to put spaces into an
Integer. But I'm sure your two method suggestions will work great.

Thanks!
-Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top