0

I have a sample of a code to convert numbers into N-base numbers.
I can have 2, 3 or 4 digits numbers.

My function works and look like this :

Private Function BaseN(s As String)
    Dim d = Val(s(s.Length - 1))
    Dim c = Val(s(s.Length - 2))
    Dim a, b As Integer
    Try
        b = Val(s(s.Length - 3))
    Catch
        b = 0
    End Try
    Try
        a = Val(s(s.Length - 4))
    Catch
        a = 0
    End Try

    Return (a * (n * n * n) + b * (n * n) + c * n + d + 1).ToString
End Function

But for a reason, like that it takes like 10 sec to convert my text.

I tried that way :

Private Function BaseN(s As String)
    Dim d = Val(s(s.Length - 1))
    Dim c = Val(s(s.Length - 2))
    Dim a, b As Integer
        b = 0
        a = 0

    Return (a * (n * n * n) + b * (n * n) + c * n + d + 1).ToString
End Function

And here the conversion is roughly instant.

So I was wondering why the try catch were so long to run, or if I've made a bad usage of them.

GSerg
  • 76,472
  • 17
  • 159
  • 346
Foxhunt
  • 764
  • 1
  • 9
  • 23
  • 2
    The second function has half as many `Val`s, and it is not clear if you are running the release build. If in the first function the `Try`s actually fire on each line of input, then yes, it may be noticeably slower, and you [should not do that](https://stackoverflow.com/a/891230/11683). – GSerg Feb 23 '19 at 12:33
  • 2
    Throwing and catching exceptions is expensive and so should only be done in exceptional circumstances. Your situation is definitely not that. Do NOT use exceptions for control flow. If you want to take a substring then make sure the `String` is long enough first. Prevention is better than cure. Don't do the wrong thing and clean up afterwards. Make sure you're doing the right thing before doing it. – jmcilhinney Feb 23 '19 at 12:33
  • Thank you, that is what i wanted to know. I though the usage of try as "IF ERROR THEN ..." was good, that's why i tried that. I'll do a simple if then – Foxhunt Feb 23 '19 at 13:09
  • @GSerg Great link. Thank you. – Mary Feb 23 '19 at 23:45
  • As a side note, `Val` is a legacy function that was suspect for use even in classic VB code due to its various quirks. I'd strongly recommend against using it. Mary's answer provides a good alternative (and there is also a `Double.TryParse` if you need to work with floating point numbers instead of integers). – Craig Feb 25 '19 at 14:18

1 Answers1

0

Just because you are not trapping errors with a Try...Catch doesn't mean you don't have to account for bogus user input. The following code should avoid crashes by checking the input for integers, and string length.

Integer.TryParse(string, integerVariable) will check the string to see if it can be converted to an Integer. It fills the integerVariable with the Integer representation of the string. The integerVariable will be zero even if it fails which makes your final equation valid.

I did not check the validity of the Function itself.

Private Function BaseN(s As String, n As Integer) As String
    Dim StringLength As Integer = s.Length
    Dim a, b, c, d As Integer
    Select Case StringLength
        Case 2
            Integer.TryParse(s(StringLength - 1), d)
            Integer.TryParse(s(StringLength - 2), c)
        Case 3
            Integer.TryParse(s(StringLength - 3), b)
            Integer.TryParse(s(StringLength - 1), d)
            Integer.TryParse(s(StringLength - 2), c)
        Case 4
            Integer.TryParse(s(StringLength - 4), a)
            Integer.TryParse(s(StringLength - 3), b)
            Integer.TryParse(s(StringLength - 1), d)
            Integer.TryParse(s(StringLength - 2), c)
        Case Else
            Return ""
    End Select
    Return (a * (n * n * n) + b * (n * n) + c * n + d + 1).ToString
End Function
Mary
  • 14,926
  • 3
  • 18
  • 27
  • This code ignores the return value from `TryParse`. If any of the characters of the string are not a digit, this code will likely return an incorrect value. – Chris Dunaway Feb 25 '19 at 15:16
  • @ChrisDunaway The OP assigns zero to the variables that fail the Val test which is exactly what happens in my code. – Mary Feb 25 '19 at 15:29