Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

Not sure if this helps, but it tells me the datatypes of the variables:

 

       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)

Posted

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!

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

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.

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:

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?

Edited by PlausiblyDamp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...