Resolved Arrays with Option Strict On

Gorkfu

New member
Joined
Oct 22, 2008
Messages
3
Programming Experience
3-5
I am new to VB.net, I've only been messing with it for a few weeks. However I'm not new to coding, been an avid php coder for the last few years. Anyways on to my issue. :p

I found some code written for VB6 for extracting text within tags. I took it and modified it for VB.net, not much was needed to be modified. I ended up with the following code.

VB.NET:
    Public Function ExtractIt(ByVal TextIN As String, Optional ByVal StartTag As String = " ", Optional ByVal EndTag As String = " ") As String
        On Error GoTo LocalError

        Dim lArray As Object
        ExtractIt = ""
        lArray = Split(TextIN, StartTag)
        If IsArray(lArray) Then
            ExtractIt = lArray(1)
            lArray = Split(ExtractIt, EndTag)
            If IsArray(lArray) Then
                ExtractIt = lArray(0)
            Else
                ExtractIt = ""
            End If
        End If
        Exit Function

LocalError:
        ExtractIt = "Didn't Work!"
    End Function
The code works fine if I have the strict option off, however I'm in the habit of leaving it on and want to keep that habit. :)

With option strict on I get an error, "Option Strict On disallows late binding." If I understand correctly it means I can't bind an array or part of an array to a variable with option strict on.

Error shows up on these lines:
ExtractIt = lArray(1)
ExtractIt = lArray(0)

How do i change the arrays to work properly with "Option Strict On"?

Original code can be found here:
Extract Text Within Tags
 
Last edited:
Hello.

Try this instead. Maybe the error occured 'cause you defined lArray as Object instead as String.

VB.NET:
    Public Function ExtractIt(ByVal TextIN As String, Optional ByVal StartTag As String = " ", Optional ByVal EndTag As String = " ") As String
        Try
            Dim lArray As String() = Split(TextIN, StartTag)
            ' ??? ExtractIt = ""

            If IsArray(lArray) Then
                ExtractIt = lArray(1)
                lArray = Split(ExtractIt, EndTag)
                If IsArray(lArray) Then
                    ExtractIt = lArray(0)
                Else
                    ExtractIt = ""
                End If
            End If
        Catch ex as Exception
            ExtractIt = "Didn't Work!"        
        End Try
    End Function

Alternative to it, you could use Regular Expressions:
VB.NET:
Imports System.Text.RegularExpressions

Public Function ExtractIt(ByVal text as String, Optional byVal StartTag as String = " ", Optional ByVal EndTag as String = " ") as List(Of String)

      Dim matches as New List(Of String)

      For each match as Match in RegEx.Matches(text, StarTag & "[a-zA-z0-9-!"#$%&'()*+,./:;<=>?@\[\\\]_`{|}~]* & EndTag)
            matches.Add(match.Value)
      Next

      Extractit = matches
End Function

But there might an issue there, if you use this function to split a string, since the last and the first word doesn't match this pattern, instead I'd use the Split() Function.

Bobby
 
Gorkfu, Option Strict disallows implicit Type conversion, but in the two error lines you make implicit conversion from object to string

First solution: convert to string
VB.NET:
ExtractIt = lArray(1).ToString()

Better solution: create an array of String (strongly typed)
VB.NET:
Dim lArray As String()
 
Thanks for the help guys, changing the lArray variable to a string fixed the problem. However, using .tostring or even Cstring to convert the arrays would not work. I'm not sure why, but maybe someone could explain that one. I actually attempted the conversions before my original post.

Now I understand the difference between implicit and explicit conversions and it helps to understand them, lol.
 
I just realized why the .tostring and Cstring conversions did not work. It's the fact that the original code was like this:

Dim lArray As Object

When it should have been:

Dim lArray As Object()
 
Back
Top