-1

I have strings stored in an array and need to check if the 5th character of each string is a number or not. The code I used is:

If Mid(arr(i), 5, 1) = IsNumeric(True) Then
      MsgBox("Number")
End If

It is giving an error:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "" to type 'Boolean' is not valid.

Community
  • 1
  • 1
Subi
  • 3
  • 1
  • 4
  • `Conversion from string "" to type 'Boolean' is not valid.` is a straight problem. You are passing a `boolean` to the function (true) instead of a object that can be evaluated as a number... that's your real problem. Also turn `Option Strict On`, it's your friend.. – Trevor Jan 24 '17 at 02:57
  • @Zaggler that's *one* problem. – Mathieu Guindon Jan 24 '17 at 02:58
  • I know, but that's the error. When that is addressed he has more... – Trevor Jan 24 '17 at 02:59

3 Answers3

3

You originally tagged your question as , but VBA doesn't throw System.InvalidCastException, or any other exception for that matter; does.

IsNumeric(True) returns True if True is numeric. You want to verify if the string retrieved from the array is numeric; give it the string retrieved from the array as a parameter:

If IsNumeric(Mid(arr(i), 4, 1)) Then
    MsgBox("Number")
End If

Your code reads like VB6/VBA though, because of this:

Imports Microsoft.VisualBasic

That namespace contains VB6-like things that you don't need to use at all. The beauty of .net is that everything is an object, so assuming the array is an array of String, you can call the actual String instance methods instead of VB6's Mid function.

Dim theFifthCharacter As String = arr(i).Substring(4, 1)

Or, because you're only interested in 1 character, and a String is itself an IEnumerable(Of Char), you can do this:

Dim theFifthCharacter As Char = arr(i)(4)

Notice the off-by-one - in .net indices start at 0, so if you want the 5th item you'll fetch index 4.

Now, if you want to see if it's numeric, you can try to parse it:

Dim digitValue As Integer
If Int32.TryParse(theFifthCharacter, digitValue) Then
    'numeric: digitValue contains the numeric value
Else
    'non-numeric: digitValue contains an Integer's default value (0)
End If

Lastly, if you want a message box, use WinForms' MessageBox instead of VB6's MsgBox:

Dim digitValue As Integer
If Int32.TryParse(theFifthCharacter, digitValue) Then
    'numeric: digitValue contains the numeric value
    MessageBox.Show(string.Format("Number: {0}", digitValue))
Else
    'non-numeric: digitValue contains an Integer's default value (0)
    MessageBox.Show("Not a number")
End If
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
1

A more correct way to do this would be:

If Char.IsDigit(arr(i)(4)) Then
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Seeing as this is idiomatic .net code that isn't using the VB6 rubbish in the `Microsoft.VisualBasic` namespace, I'm very curious about what the reason for the downvote here might be. – Mathieu Guindon Jan 24 '17 at 02:59
  • 1
    Using `Char.IsDigit()` allows some characters to return true and they are not even in the correct ASCII range. My opinion, `Integer.TryParse` is a better approach, ensures that it definitely falls between 0-9... Also explaining why the OP has that error would be useful so the OP can understand the problem. – Trevor Jan 24 '17 at 03:14
  • @Zaggler, I think that you are confusing `Char.IsDigit` with `Char.IsNumber`. According to the documentation, `IsDigit` will only match 0 - 9. – jmcilhinney Jan 24 '17 at 04:00
  • Char.IsDigit is a subset of Char.IsNumber... and depending on environment, some Thai symbols return as digit and it's wrong.. IsDigit can be useful but at the same time it doesn't hold up in cases. – Trevor Jan 24 '17 at 04:05
0

Your syntax is incorrect. It should be:

If IsNumeric(Mid(arr(i), 5, 1)) Then
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272