Vartype Function

EGGMAN

Newcomer
Joined
May 22, 2003
Messages
5
Location
Spencer, MA
I am trying to convert a vb6 app to vb.net and the vartype function is giving me trouble, for example:

Dim x As String
Dim y As String = ""

MsgBox(VarType(x)) 'Returns 9 (object)
MsgBox(VarType(y)) 'Returns 8 (string)

Does anyone know a way of getting the data type of a variable without initializing it?
 
Not sure if this helps, but it tells me the datatypes of the variables:

Code:
        Dim x As Object
        Dim y As String = ""

        Dim n As Microsoft.VisualBasic.VariantType
        Dim m As Microsoft.VisualBasic.VariantType

        n = VarType(x)
        m = VarType(y)

        MessageBox.Show("X: " & n.ToString & ControlChars.CrLf & "Y: " & m.ToString)
 
I tried your code out, but if you don't initialize the string to "", you still get the same results.

This problem only comes into play when I am passing an uninitialized string variable to a sub with an object type paramenter. Inside this sub, I need to know what data type is defined as outside the sub.

Thanks for your efforts!
 
Seems to be a consistent problem though, as well as VarType not giving accurate results

.GetType reports System.Object for an uninitiallised string and also 'Is TypeOf' returns false as well.

Seems to be a bit of a problem if the string is Nothing, not too sure of a way round it though
 
Well, this code was from and old vb6 program that I want to convert to .NET. The purpose of this function was to pass it a field from an ado recordset and the variable you want to load it into. So for example, if the ado field had a null in it, and you passed it a string it would assign it an empty string, etc.
Visual Basic:
Public Sub GetSQLFld(F As ADODB.Field, x As Variant)
    
    Dim TargetType As Integer
    Dim SQLType As Integer
    
    TargetType = VarType(x)
    SQLType = VarType(F)

    If SQLType = vbNull Then
        Select Case TargetType
            Case vbString
                x = ""
            Case vbBoolean
                x = False
            Case vbVariant, vbDate
                x = Null
            Case Else
                x = 0
        End Select
    Else
        If TargetType = vbString Then
            x = UCase$(RTrim$(F))
        Else
            x = F
        End If
    End If
End Sub
However, when it got converted to .NET, x was converted to an "OBJECT". So unless the variable is initialized before you pass it to this sub, the vartype function cannot determine what type it is defined as. VarType(x) always comes back as type object.
I really don't want to initialize all the varibles before calling this sub it possible.

This is how vb.net converted the sub:
Visual Basic:
Public Sub GetSQLFld(ByRef F As ADODB.Field, ByRef X As Microsoft.VisualBasic.VariantType)

        Dim TargetType As Short
        Dim SQLType As Short

        'UPGRADE_WARNING: VarType has a new behavior. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1041"'
        TargetType = VarType(X)
        'UPGRADE_WARNING: VarType has a new behavior. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1041"'
        SQLType = VarType(F.Value)

        If SQLType = VariantType.Null Then
            Select Case TargetType
                Case VariantType.String
                    'UPGRADE_WARNING: Couldn't resolve default property of object X. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
                    X = ""
                Case VariantType.Boolean
                    'UPGRADE_WARNING: Couldn't resolve default property of object X. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
                    X = False
                Case VariantType.Object, VariantType.Date
                    'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1049"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object X. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
                    X = VariantType.Null
                Case Else
                    'UPGRADE_WARNING: Couldn't resolve default property of object X. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
                    X = 0
            End Select
        Else
            If TargetType = VariantType.String Then
                'UPGRADE_WARNING: Couldn't resolve default property of object X. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
                X = UCase(RTrim(F.Value))
            Else
                'UPGRADE_WARNING: Couldn't resolve default property of object X. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
                X = F.Value
            End If
        End If
    End Sub
Maybe there is a better way of doing this?
 
Last edited by a moderator:
Back
Top