Home All Groups Group Topic Archive Search About

VB6 Variable Name as String

Author
12 Oct 2005 4:29 PM
Jonathan
I am using Visual Basic 6 and I would like to know if there is a way to get
the value of a variable from the variable name as a string.

For example:

Dim i as integer

i = 5

MsgBox GetValue("i")

Then the message box would say 5. The reason I want to do this is so I can
show the value of my variables remotely for debugging purposes. I have found
a data server but it needs the variable name as a string. I have thousands of
variables so I want to avoid typing out the name if possible.

Also, is there a way to request all of the global variables in my project in
an array? These are the ones I am really interested in. For example:

Dim D() as Variant
Dim i as integer

D = GetGlobals()

For i = 0 to UBound(D) - 1
    ...... Manipulate data .....
Next i

I know that this is awkward, but I am looking for an easy to output my
variable names and values for debug purposes. Thanks for the help.

Author
12 Oct 2005 5:17 PM
Ralph
Show quote Hide quote
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:27912B42-E5FE-41F4-9BE1-79FE437F8DE5@microsoft.com...
> I am using Visual Basic 6 and I would like to know if there is a way to
get
> the value of a variable from the variable name as a string.
>
> For example:
>
> Dim i as integer
>
> i = 5
>
> MsgBox GetValue("i")
>
> Then the message box would say 5. The reason I want to do this is so I can
> show the value of my variables remotely for debugging purposes. I have
found
> a data server but it needs the variable name as a string. I have thousands
of
> variables so I want to avoid typing out the name if possible.
>
> Also, is there a way to request all of the global variables in my project
in
> an array? These are the ones I am really interested in. For example:
>
> Dim D() as Variant
> Dim i as integer
>
> D = GetGlobals()
>
> For i = 0 to UBound(D) - 1
>     ...... Manipulate data .....
> Next i
>
> I know that this is awkward, but I am looking for an easy to output my
> variable names and values for debug purposes. Thanks for the help.

VB is not strongly typed in the traditional way. For example, many
functions/objects actually take 'Variants'. Try just typing "msgbox i" and
see what you get. Or TextBox1.Text = i.

This phenomena is part of VB's inherent 'type-coercion', or sometimes "VB's
evil type-coercion". <g>

To specifically force a specific type use a Type Conversion Function ( CStr,
CLng, ...) In this case the Str() may be appropriate (also take a peek at
Val(), Asc() in your help)

hth
-ralph
Author
12 Oct 2005 5:37 PM
Someone
You could use a type, like:

Public Type MyGlobals
    Var1 As Long
    Var2 As Long
    '
    '
End Type

Public g As MyGlobals

Then you can access them like this with intellisence:

g.Var1 = 1

Another suggestion is to copy all of your variables to a new Module/Project
for editing purposes. Use Find and Replace and leave the Replace field
blank, and Replace all instances of "Public ". To get rid of "As xxxx", in
Find type "As *" and enable "Use Pattern Matching". Make sure that you
select "Current Procedure", otherwise it will mess up with your other files.
It's best to use a new project just to be on the safe side.

To get rid of the extra space around the variables, use Excel. Paste the
result into cell A1 in Excel. In cell B1 type "=Trim(A1)". Now highlight
cells B1 to Bxxx and select Edit|Fill|Down.

If you don't have Excel, you could use EasySpreadsheet which is part of
EasyOffice freeware.

http://www.download.com/EasyOffice-with-PDF-Filter/3000-2079_4-10414633.html



Show quoteHide quote
"Ralph" <nt_consultin***@yahoo.com> wrote in message
news:b46dnYhm5-Ka2NDeRVn-jg@arkansas.net...
>
> "Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
> news:27912B42-E5FE-41F4-9BE1-79FE437F8DE5@microsoft.com...
>> I am using Visual Basic 6 and I would like to know if there is a way to
> get
>> the value of a variable from the variable name as a string.
>>
>> For example:
>>
>> Dim i as integer
>>
>> i = 5
>>
>> MsgBox GetValue("i")
>>
>> Then the message box would say 5. The reason I want to do this is so I
>> can
>> show the value of my variables remotely for debugging purposes. I have
> found
>> a data server but it needs the variable name as a string. I have
>> thousands
> of
>> variables so I want to avoid typing out the name if possible.
>>
>> Also, is there a way to request all of the global variables in my project
> in
>> an array? These are the ones I am really interested in. For example:
>>
>> Dim D() as Variant
>> Dim i as integer
>>
>> D = GetGlobals()
>>
>> For i = 0 to UBound(D) - 1
>>     ...... Manipulate data .....
>> Next i
>>
>> I know that this is awkward, but I am looking for an easy to output my
>> variable names and values for debug purposes. Thanks for the help.
>
> VB is not strongly typed in the traditional way. For example, many
> functions/objects actually take 'Variants'. Try just typing "msgbox i" and
> see what you get. Or TextBox1.Text = i.
>
> This phenomena is part of VB's inherent 'type-coercion', or sometimes
> "VB's
> evil type-coercion". <g>
>
> To specifically force a specific type use a Type Conversion Function (
> CStr,
> CLng, ...) In this case the Str() may be appropriate (also take a peek at
> Val(), Asc() in your help)
>
> hth
> -ralph
>
>
Author
12 Oct 2005 5:45 PM
Rick Rothstein [MVP - Visual Basic]
> blank, and Replace all instances of "Public ". To get rid of "As
xxxx", in
> Find type "As *" and enable "Use Pattern Matching". Make sure
that you
> select "Current Procedure", otherwise it will mess up with your
other files.

And, if you Open any files in that Procedure, be aware of As
keyword in the Open declaration.

Rick
Author
12 Oct 2005 6:05 PM
Ralph
"Someone" <nob***@cox.net> wrote in message
news:HQb3f.5539$MN6.5500@fed1read04...
Show quoteHide quote
> You could use a type, like:
>
> Public Type MyGlobals
>     Var1 As Long
>     Var2 As Long
>     '
>     '
> End Type
>
> Public g As MyGlobals
>
> Then you can access them like this with intellisence:
>
> g.Var1 = 1
>
> Another suggestion is to copy all of your variables to a new
Module/Project
> for editing purposes. Use Find and Replace and leave the Replace field
> blank, and Replace all instances of "Public ". To get rid of "As xxxx", in
> Find type "As *" and enable "Use Pattern Matching". Make sure that you
> select "Current Procedure", otherwise it will mess up with your other
files.
> It's best to use a new project just to be on the safe side.
>
> To get rid of the extra space around the variables, use Excel. Paste the
> result into cell A1 in Excel. In cell B1 type "=Trim(A1)". Now highlight
> cells B1 to Bxxx and select Edit|Fill|Down.
>
> If you don't have Excel, you could use EasySpreadsheet which is part of
> EasyOffice freeware.
>
>
http://www.download.com/EasyOffice-with-PDF-Filter/3000-2079_4-10414633.html
Show quoteHide quote
>
>
>
> "Ralph" <nt_consultin***@yahoo.com> wrote in message
> news:b46dnYhm5-Ka2NDeRVn-jg@arkansas.net...
> >
> > "Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
> > news:27912B42-E5FE-41F4-9BE1-79FE437F8DE5@microsoft.com...
> >> I am using Visual Basic 6 and I would like to know if there is a way to
> > get
> >> the value of a variable from the variable name as a string.
> >>
> >> For example:
> >>
> >> Dim i as integer
> >>
> >> i = 5
> >>
> >> MsgBox GetValue("i")
> >>
> >> Then the message box would say 5. The reason I want to do this is so I
> >> can
> >> show the value of my variables remotely for debugging purposes. I have
> > found
> >> a data server but it needs the variable name as a string. I have
> >> thousands
> > of
> >> variables so I want to avoid typing out the name if possible.
> >>
> >> Also, is there a way to request all of the global variables in my
project
> > in
> >> an array? These are the ones I am really interested in. For example:
> >>
> >> Dim D() as Variant
> >> Dim i as integer
> >>
> >> D = GetGlobals()
> >>
> >> For i = 0 to UBound(D) - 1
> >>     ...... Manipulate data .....
> >> Next i
> >>
> >> I know that this is awkward, but I am looking for an easy to output my
> >> variable names and values for debug purposes. Thanks for the help.
> >
> > VB is not strongly typed in the traditional way. For example, many
> > functions/objects actually take 'Variants'. Try just typing "msgbox i"
and
> > see what you get. Or TextBox1.Text = i.
> >
> > This phenomena is part of VB's inherent 'type-coercion', or sometimes
> > "VB's
> > evil type-coercion". <g>
> >
> > To specifically force a specific type use a Type Conversion Function (
> > CStr,
> > CLng, ...) In this case the Str() may be appropriate (also take a peek
at
> > Val(), Asc() in your help)
> >
> > hth
> > -ralph
> >

Good point. I forgot about his second question.

I have been known to create 'test_bed' functions for debugging, especially
for classes. But it will work for modules just as well.
It takes a little typing but can be useful.

' using Someone's scheme
Private Type MyGlobals
     Var1 As Long
     Var2 As Long
End Type
Dim glob_t As MyGlobals

Dim globalValue1
Dim globalValue2
Dim globalValue3

#Const DebugMode = 1
Public Function ShowState() As Boolean
#If DebugMode Then
    ' list all your members, globals here
    Debug.Print "globalValue1 = " & CStr(globalValue1)
    Debug.Print "globalValue2 = " & CStr(globalValue2)
    Debug.Print "globalValue3 = " & CStr(globalValue3)
    With glob_t
        Debug.Print "glob_t values " & _
                           "     Var1 = " & CStr( .Var1) & _
                          "      Var2 = " & CStr(.Var2)
    End With
#End If
End Function

' now in code somewhere ....
    With glob_t
        .Var1 = 5
        .Var2 = 3
    End With
    globalValue1 = 23
    globalValue2 = "this is value2"
    globalValue3 = "this is value 3"

    Debug.Assert ShowState    ' will show all the values.
....
It seems like a bit of work, but if you keep up with it, it becomes a minor
chore. When you are ready for production just set DebugMode to zero.

-ralph
Author
12 Oct 2005 7:26 PM
Someone
I missed printing the values part. In VB6, you could use Debug|Add Watch,
and add the "g" variable, because it's a UDF, VB6 will show all the contents
inside it and their current value.

If you want to make a series of Debug.Print, then use Excel as follows:

- Assuming that you followed the previous steps in making your variables
without spaces as shown in my previous post, go to cell C1 and type:

=CONCATENATE("Debug.Print ", CHAR(34), B1, " = ", CHAR(34), "; .", B1)

- Copy cell C1 and paste to cells C2 to Cxxx.

The above will generate:

Debug.Print "Var1 = "; .Var1

When you copy and paste the formula, Excel will adjust the cell reference so
all Debug.Print are generated for you. If you have a UDF within a UDF, then
you have to do part of it manually. Usually you would press "." at the end
of the variable.

- Copy column C and paste into your code. Use "With g" at the beginning of
the block.

Note that using "g" as a global variable can cause problems, so change it to
something more unique, like "_g", "g_", etc.



"Someone" <nob***@cox.net> wrote in message
news:HQb3f.5539$MN6.5500@fed1read04...
Show quoteHide quote
> You could use a type, like:
>
> Public Type MyGlobals
>    Var1 As Long
>    Var2 As Long
>    '
>    '
> End Type
>
> Public g As MyGlobals
>
> Then you can access them like this with intellisence:
>
> g.Var1 = 1
>
> Another suggestion is to copy all of your variables to a new
> Module/Project for editing purposes. Use Find and Replace and leave the
> Replace field blank, and Replace all instances of "Public ". To get rid of
> "As xxxx", in Find type "As *" and enable "Use Pattern Matching". Make
> sure that you select "Current Procedure", otherwise it will mess up with
> your other files. It's best to use a new project just to be on the safe
> side.
>
> To get rid of the extra space around the variables, use Excel. Paste the
> result into cell A1 in Excel. In cell B1 type "=Trim(A1)". Now highlight
> cells B1 to Bxxx and select Edit|Fill|Down.
>
> If you don't have Excel, you could use EasySpreadsheet which is part of
> EasyOffice freeware.
>
> http://www.download.com/EasyOffice-with-PDF-Filter/3000-2079_4-10414633.html
>
>
>
> "Ralph" <nt_consultin***@yahoo.com> wrote in message
> news:b46dnYhm5-Ka2NDeRVn-jg@arkansas.net...
>>
>> "Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
>> news:27912B42-E5FE-41F4-9BE1-79FE437F8DE5@microsoft.com...
>>> I am using Visual Basic 6 and I would like to know if there is a way to
>> get
>>> the value of a variable from the variable name as a string.
>>>
>>> For example:
>>>
>>> Dim i as integer
>>>
>>> i = 5
>>>
>>> MsgBox GetValue("i")
>>>
>>> Then the message box would say 5. The reason I want to do this is so I
>>> can
>>> show the value of my variables remotely for debugging purposes. I have
>> found
>>> a data server but it needs the variable name as a string. I have
>>> thousands
>> of
>>> variables so I want to avoid typing out the name if possible.
>>>
>>> Also, is there a way to request all of the global variables in my
>>> project
>> in
>>> an array? These are the ones I am really interested in. For example:
>>>
>>> Dim D() as Variant
>>> Dim i as integer
>>>
>>> D = GetGlobals()
>>>
>>> For i = 0 to UBound(D) - 1
>>>     ...... Manipulate data .....
>>> Next i
>>>
>>> I know that this is awkward, but I am looking for an easy to output my
>>> variable names and values for debug purposes. Thanks for the help.
>>
>> VB is not strongly typed in the traditional way. For example, many
>> functions/objects actually take 'Variants'. Try just typing "msgbox i"
>> and
>> see what you get. Or TextBox1.Text = i.
>>
>> This phenomena is part of VB's inherent 'type-coercion', or sometimes
>> "VB's
>> evil type-coercion". <g>
>>
>> To specifically force a specific type use a Type Conversion Function (
>> CStr,
>> CLng, ...) In this case the Str() may be appropriate (also take a peek at
>> Val(), Asc() in your help)
>>
>> hth
>> -ralph
>>
>>
>
>
Author
12 Oct 2005 6:00 PM
Jeff Johnson [MVP: VB]
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:27912B42-E5FE-41F4-9BE1-79FE437F8DE5@microsoft.com...

>I am using Visual Basic 6 and I would like to know if there is a way to get
> the value of a variable from the variable name as a string.

No.
Author
12 Oct 2005 6:15 PM
Ken Halter
Show quote Hide quote
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:27912B42-E5FE-41F4-9BE1-79FE437F8DE5@microsoft.com...
>I am using Visual Basic 6 and I would like to know if there is a way to get
> the value of a variable from the variable name as a string.
>
> For example:
>
> Dim i as integer
>
> i = 5
>
> MsgBox GetValue("i")
>
> Then the message box would say 5. The reason I want to do this is so I can
> show the value of my variables remotely for debugging purposes. I have
> found
> a data server but it needs the variable name as a string. I have thousands
> of
> variables so I want to avoid typing out the name if possible.
>
> Also, is there a way to request all of the global variables in my project
> in
> an array? These are the ones I am really interested in. For example:
>
> Dim D() as Variant
> Dim i as integer
>
> D = GetGlobals()
>
> For i = 0 to UBound(D) - 1
>    ...... Manipulate data .....
> Next i
>
> I know that this is awkward, but I am looking for an easy to output my
> variable names and values for debug purposes. Thanks for the help.

There's no such functionality built in.

I have a Debug.Print "work-a-like" that dumps to a file (and/or the debug
window)

When I need the values of variables, I do something like....

DebugPrint "i = " & i
DebugPrint "ThisVar = " & ThisVar

For x = 1 to 10
    DebugPrint "ThisArrayVar(" & x & ") = " & ThisArrayVar(x)
Next

etc, etc....

It may seem like a lot of work but it's pretty fast to setup. Just
copy/paste all variables you want to watch into the immediate window, create
one line of code, copy/paste that several times and then cut/paste the real
variable names into the code.

You can include/exclude the debug code from your project with conditional
compilation.

About the only way I know of that doesn't require this kind of work would be
to use the fairly expensive tool here...

VB Watch
http://www.aivosto.com/vbwatch.html

fwiw, it doesn't just "work", you have to click a button or two to get the
addin to add the required code to your project.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
12 Oct 2005 6:33 PM
MikeD
Show quote Hide quote
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:27912B42-E5FE-41F4-9BE1-79FE437F8DE5@microsoft.com...
>I am using Visual Basic 6 and I would like to know if there is a way to get
> the value of a variable from the variable name as a string.
>
> For example:
>
> Dim i as integer
>
> i = 5
>
> MsgBox GetValue("i")
>
> Then the message box would say 5. The reason I want to do this is so I can
> show the value of my variables remotely for debugging purposes. I have
> found
> a data server but it needs the variable name as a string. I have thousands
> of
> variables so I want to avoid typing out the name if possible.


Not really.  When the app is compiled, the variable name is replaced with a
memory address.  If you were only talking about one or two variables, I'd
say wrap them as properties and then you could use the CallByName function.
This would hardly be practicle for thousands of variables, though. Here's an
example of what I'm talking about, just in case you're interested anyways.

Option Explicit

Private MyVariable As Long

Public Property Get MyProperty() As Long

    MyProperty = MyVariable

End Property

Public Property Let MyProperty(ByVal NewValue As Long)

    MyVariable = NewValue

End Property

Private Sub Form_Click()

    MsgBox CallByName(Me, "MyProperty", VbGet)

End Sub

Private Sub Form_Load()

    MyProperty = 5
    'Or you could use the variable itself

End Sub

Even so, you probably would not want to do this soley for debugging purposes
unless the app was a beta or debug version (IOW, non-production).


Hmm.  One thing I did just think of the *may* help would be to create
symbolic debug information during compilation.  I don't know if this will
allow you to do what you want or not as I've never used this feature in VB.
If you need more info on this, see the following KB articles:

How To Compile VB Programs with Debug Symbols Embedded
http://support.microsoft.com/default.aspx?scid=kb;en-us;193133

How To Debug a Native Code Visual Basic Component in VC++
http://support.microsoft.com/default.aspx?scid=kb;en-us;166275

How To Debug Visual Basic COM Components That Are Used Within Active Server
Pages
http://support.microsoft.com/default.aspx?scid=kb;en-us;299633

You mentioned remote debugging and using a data server, so I don't know if
any of these will really help you or not.


--
Mike
Microsoft MVP Visual Basic