Global Variables

Here is a question that might be impossible from VB.  I would like to be able to display a list of my current global variable names and VALUES during run time from my program?  My idea is to load the variables names from a .txt file and load to an array and display within a listview.  I am having a hard time displaying the actual value.  There are all either integer, boolean, or strings.
LVL 4
PBuckAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mackagpCommented:
oh for sinclair basic where the val command worked with variable names!!!!
0
amebaCommented:
' This is solution for Form Global variables
Option Explicit
Public x1 As String
Public x2 As Long
Public x3 As Boolean

Private Sub Form_Click()
    ' your variables
    x1 = "str"
    x2 = 10
    x3 = True
    ' get variable type and value
    test "x1"
    test "x2"
    test "x3"
End Sub

Public Sub test(VarName As String)
    Dim tempvar As Variant
    tempvar = CallByName(Me, VarName, VbGet)
    ' report type and value
    MsgBox TypeName(tempvar) & ": " & vbCrLf & _
        tempvar, vbInformation, VarName
End Sub
0
amebaCommented:
for Project Global variables:

Since CallByName works only with objects
    tempvar = CallByName(Me, VarName, VbGet)
you will have to use class, e.g Class1:
    tempvar = CallByName(Class1, VarName, VbGet)
And all your global variables must be in that class with property Instancing=6 (GlobalMultiUse)

Your Project must be ActiveX Exe or DLL (cannot be Standard Exe, because Standard Exe does not support GlobalMultiUse instancing).

Variables in defined GlobalMultiUse class are used the same way you use 'normal' global variables in module:
  x = 10
No need to use:
  Class1.x = 10
and you don't need to instantiate (create) GlobalMultiUse class.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mcriderCommented:
CallByName is only available in VB6...

For VB5 You would have to code a subroutine with a case statement for all of your global variables...


Public Sub test(VarName As String)
    Dim tempvar As Variant
   
    Select Case VarName
        Case "xxx"
            tempvar = xxx
        Case "yyy"
            tempvar = yyy
    End Select
    ' report type and value
    MsgBox TypeName(tempvar) & ": " & vbCrLf & _
        tempvar, vbInformation, VarName
End Sub


Cheers!
0
FoyalCommented:
One simple way is to use the CStr function when displaying the values. It will convert any valid numeric or string expression to a string. This CStr function is also "data aware" so that the format is correct for the locale of the system in use.

Like this:
    If Data <> Null then
    txtData = CStr(Data)

(Null is the only case where a runtime error is set)
Boolean Types return either "True" or "False"
Dates are returned as a short-date according to your system
Empty returns an empty string
Other numerics return a string containing the number
0
amebaCommented:
This is R16.
0
PBuckAuthor Commented:
Sorry it has taken me so long to respond back.  Thanks to everyone who has helped with my VB problem.

Ameba's Form Global answer best fits my needs for my current project.
0
PBuckAuthor Commented:
I tried using CStr prior to sending this question up.  It didn't work as I thought it would using a array:  Cstr(Global_Var(X))

Thanks.

0
amebaCommented:
>Ameba's Form Global answer best fits my needs for my current project.
Glad to help!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amebaCommented:
B :(
0
mcriderCommented:
Asked 1... Graded 1... B...
0
PBuckAuthor Commented:
Heeheehee ... it was a good answer for Form Global varaibles, but not for my Project Global variables.  I am unsure what the big difference between ActiveX.exe and Standard.exe (able to pass variables to and from)?

Plus, it uses a VB6 command only.  I didn't care to paste all my variables within a case statement (like mcrider suggested - it would have worked also).  Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.