|
-
Sep 29th, 2000, 05:46 AM
#1
Thread Starter
Junior Member
Hello Everyone,
I've been trying to find out if it's possible (and if it is how) to take the data in a String which is a name of another var, and save data to that other var...
Exampe:
Dim Temp as string
Dim AnotherVar as integer
Temp = "AnotherVar"
*** Lets say I want to save in AnotherVar the value "7", I don't want to write AnotherVar = 7, I want to find a way that I can do TheDataInTemp = 7
And since the Data in temp is the word "AnotherVar", it will save the value 7 in the AnotherVar variable that I defined at the begining...
Any Ideas?
-
Sep 29th, 2000, 06:31 AM
#2
Guru
You can't. 
If we whine a lot, maybe it will be possible in VB7.
-
Sep 29th, 2000, 06:41 AM
#3
Thread Starter
Junior Member
Oh well,
Guess it's back to the ol' "case" command to do what I was thinking off...
Thanks Yonatan.
Btw: Any info about VB7's release date?
-
Sep 29th, 2000, 08:34 AM
#4
I believe what you want was called dynamic evaluation and was available in ALL the old database languages (DB, Revelation, clairion, RBase....), but these were all interpreted languages. Essentially, you put the command string into a string variable and invoked the execution function using the string as an argument.
At the time I studied compiliers (much longer ago than I wish to remember) there was a reason why this facility was not included. But I can't remember........
I have done something similiar involving tables and data, but it was kludgy and not worth the effort. If its really important, I could probably refine it.
Good Luck
DerFarm
-
Sep 29th, 2000, 10:56 AM
#5
Frenzied Member
heh, once I had the same question and started writing code to handle that situation..I ended up with a scripting language and interpreter. Its a mess to try it...I say stick with Case and wait for VB7.
-
Sep 30th, 2000, 08:42 PM
#6
Addicted Member
Hi.
If i understand you correctly!
I had the same Q a while ago. Check this post :
http://forums.vb-world.net/showthrea...threadid=22984
Sascha's method did the trick.
I use it, and it's working.
Good Luck.
-
Oct 1st, 2000, 01:00 AM
#7
Hyperactive Member
Indirection can be achieved
If this is what you want. To take you literally, the other
var is already defined, so you are not really trying to
create new variables during runtime which I think a few of
the ohter replies assume.
To use the contents of a string to determine which of
several variables to change, is easiest to do with a big
case statement. Of course, I assume you do not want this
because perhaps you want to set/get this value from many
places in your code.
Although I cannot see any possible reason why you would
absolutely need this (there are always alternatives), I
have put together a simple piece of demo code which does
the following:
1) User selects a variable name from a combo
box (names hard coded in the list for the demo but you can
easily make the list dynamic)
2) User enters an integer value in the text box
3) User hits the command button, and the appropriate var is
updated . This is achieved without the use of any testing
of the user selection.
To use this in your code, you would have to do as I have
done and give VB an alternative way to address the var you
are after.
This method is only one such way to do this.
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Dim AnotherVar As Integer
Dim AnotherVar2 As Integer
Dim sillyCol As Collection
Private Sub Command1_Click()
Debug.Print AnotherVar, AnotherVar2
' set whichever var is selected form the combo box
' to the contents of the text box.
On Error Resume Next
Dim val As Integer
val = CInt(Text1.Text)
' ignore conversion errors - leave as 0
If Err <> 0 Then Err.Clear
Dim myPointer As Long
myPointer = CLng(sillyCol(Combo1.Text))
If Err <> 0 Then
MsgBox "the variable selected cannot be found in the collection"
End
End If
CopyMemory ByVal myPointer, val, 2
Debug.Print AnotherVar, AnotherVar2
On Error GoTo 0
End Sub
Private Sub Form_Load()
Set sillyCol = New Collection
' some test integers
AnotherVar = -23
AnotherVar2 = 1023
' record the varname against it's pointer
sillyCol.Add Str(VarPtr(AnotherVar)), "AnotherVar"
sillyCol.Add Str(VarPtr(AnotherVar2)), "AnotherVar2"
End Sub
Test it out for yourself.
Cheers
-
Oct 2nd, 2000, 09:24 PM
#8
Fanatic Member
>Dim Temp as string
>Dim AnotherVar as integer
>Temp = "AnotherVar"
>*** Lets say I want to save in AnotherVar the value "7", I
>don't want to write AnotherVar = 7, I want to find a way
>that I can do TheDataInTemp = 7
>And since the Data in temp is the word "AnotherVar", it
>will save the value 7 in the AnotherVar variable that I
>defined at the begining...
>Any Ideas?
Well, I stumbled across something ... it may not be the best
way to go about it, but it does indeed work.
Create a Class Called Class1;
Code:
'Setup as many of your variables here as you want. I chose
'to use variants (I'm lazy ;>) Make sure you set them all
'up with properties as well (the name of your variable)
'I'm using your example.
Option Explicit
Private vAnotherVar As Variant
Public Property Let AnotherVar(tmpVar As Variant)
vAnotherVar = tmpVar
End Property
Public Property Get AnotherVar() As Variant
AnotherVar = vAnotherVar
End Property
On your form, add a nifty command botton called: Command1:
Code:
Option Explicit
Dim MyClass As New Class1
Dim Temp As String
Dim Data As String
Private Sub Command1_Click()
Temp = InputBox("Enter a Variable Name (String)" & _
vbCrLf & "No matter what's entered, it'll & _
go to AnotherVar :)")
Data = InputBox("Enter a String. Enter nothing to _
clear the variable.")
Temp = "AnotherVar"
CallByName MyClass, Temp, VbLet, Data
MsgBox CallByName(MyClass, Temp, VbGet)
End Sub
This is a nifty little Function/Statement if I do say so
myself. (made a lot of happy happies in my current project!)
What's happening is this: CallByName can be used as either a
function or as a Statement (Sub). It needs the following
parameters as follows:
Code:
variable = CallByName(object, method/property, method type, arguement1, arguement2, ..., argumentN)
CallByName object, method/property, method type, arguement1, arguement2, ..., arguementN
Object is a form, control, or object (hence myclass) Class
modules need to be declared before CallByName will work with
them and their methods/properties must be declared as
public.
Method/Property is the Function/Sub/Property you wish to
execute in the Object. That's where your variable name comes
into play. Set it up as a Property and you can call whatever
variable you want as long as the property name is the same
as the variable you wish to change during runtime with a
string.
Method Type is either vbGet, vbSet, vbLet, or vbMethod. Get
& Let are used with properties, get & set are used with
embedded objects, and method is used for functions & subs.
Arguement is an array which contains the data you wish to
pass to the property/method. You don't need the parens '()'
just seperate the arguments with commas.
So, there you have it. It's a round about way of doing it,
but it's the best thing that I found in VB that doesn't
require you to declare API stuff ;)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|