Re: Dynamic variable



"Simon" <spambucket@xxxxxxxxxxx> wrote in message
news:3it71cFn4ihuU1@xxxxxxxxxxxxxxxxx
> Hi,
>
> I was wondering if there was a way of doing something like...
>
> /////////////////////////////////////////////
>
> Dim g_ValA ' global values set somewhere
> Dim g_ValB
> Dim g_ValC
> Dim g_ValD
>
> Function GetValue( sItem )
> GetValue = {sItem}.Value
> End Function
>
> ...
> Function SomeOtherFunction()
>
> GetValue( "g_ValA" ) ' return the value of g_ValA
> GetValue( "g_ValB" ) ' return the value of g_ValB
> GetValue( "g_ValZ" ) ' return an error of some sort as g_ValZ
> does not exist.
>
> End Function
> /////////////////////////////////////////////
>
> where {sItem}is the name of a variable.
> I think it can be acheived in PHP by doing something lke ${val}
>
> Can it be done in VBScript?
>
> Many thanks in advance.
>
> Simon
>

Look at "Eval()".


Option Explicit

Dim g_ValA
g_ValA = "A"
Dim g_ValB
g_ValB = "B"
Dim g_ValC
g_ValC = "C"
Dim g_ValD
g_ValD = "D"

Function GetValue( sItem )
GetValue = Eval(sItem)
End Function

WScript.Echo GetValue( "g_ValA" ) ' return the value of g_ValA
WScript.Echo GetValue( "g_ValB" ) ' return the value of g_ValB
WScript.Echo GetValue( "g_ValZ" ) ' return NOTHING as g_ValZ does not
exist.


.



Relevant Pages

  • Dynamic variable
    ... Dim g_ValA ' global values set somewhere ... Function GetValue(sItem) ... Simon ... Prev by Date: ...
    (microsoft.public.scripting.vbscript)
  • Re: Dynamic variable
    ... >> Dim g_ValA ' global values set somewhere ... >> Function GetValue(sItem) ... i am using VBScript in my C++ program to allow users to run ... Simon ...
    (microsoft.public.scripting.vbscript)
  • Re: How to return a value from a function?
    ... Function GetValue() as Long ... Dim i as Long ... the answer is Exit Function. ... How can I do the same thing in Excel VBA? ...
    (microsoft.public.excel.programming)
  • Re: Snaking Column Headers
    ... It's really confusing! ... Dim dLastLeft As Double ... Dim sItem As String ... If Me.Left dLastLeft Then ...
    (microsoft.public.access.reports)
  • RE: Re : Excel Range of Values Amidst Characteristic Transitions
    ... Dim Curr As Range, Prev ... If RowArray1 Is Nothing Then ...
    (microsoft.public.excel)

Loading