I have a variable defined like this:
Type MyVar
V1 as Integer
V2 as String
V3 as ..
...
Vx as ...
End Type
I want to call a subroutine with the name of the sub variables and get the value back...
So this call: MySub("V7"), should return the value in MyVar.V7
Is this possible? I Suspect I need some 'indirect' approach...
How to address a variable
-
- BronzeLounger
- Posts: 1290
- Joined: 03 Feb 2010, 19:59
- Location: Terneuzen, the Netherlands
-
- Administrator
- Posts: 78789
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to address a variable
MyVar is a type, not a variable. So MyVar.V7 doesn't have a value.
If you have a variable W of type MyVar, W.V7 has a value.
But as far as I know, you can't refer to members of W indirectly.
Perhaps an array would be more suitable for your purpose.
If you have a variable W of type MyVar, W.V7 has a value.
But as far as I know, you can't refer to members of W indirectly.
Perhaps an array would be more suitable for your purpose.
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1290
- Joined: 03 Feb 2010, 19:59
- Location: Terneuzen, the Netherlands
Re: How to address a variable
Yes, sorry, that's how I do it. Pity it can't be done... now I have to be 'creative' somehow (of use an array, but that would require complete code rewrite..)
-
- Administrator
- Posts: 78789
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to address a variable
You could use a Select Case, but that seems rather cumbersome to me:
An array is *much* simpler. Or perhaps a collection or dictionary object? You can refer to a member of a collection/dictionary through its key (a string).
Code: Select all
Function GetVal(MemberName As String) As Integer
Select Case MemberName
Case "V1"
GetVal = W.V1
Case "V2"
GetVal = W.V2
Case "V3"
GetVal = W.V3
' etc etc
End Select
End Sub
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 818
- Joined: 24 Jan 2010, 15:56
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: How to address a variable
I was about to try callbyname. Doesn't it work for a variable?
-
- 5StarLounger
- Posts: 818
- Joined: 24 Jan 2010, 15:56
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands