Link to home
Start Free TrialLog in
Avatar of vindevogel
vindevogel

asked on

Evalutating strings to vars

I've written some stuff in VB6

1) I have a Resource file with   100, "My Name"    in the string table
2) I wrote (in a module) : Public Const sMyName as Long = 100
3) I wrote (in a form) : Me.Caption = LoadResString(sMyName)

Now, what I want ...
I want to place "sMyName" (exactly that) in the TAG property.
I want a function like this :

Dim vControl as Control

For Each vControl in Form.Controls
   If vControl.Tag <> "" Then
      vControl.Caption = LoadResString(INeedThisAsTheNumberAndNotTheName(vControl.Tag))
   End If
Next

Where of course vControl.Tag should not be inserted, but the number in the constant whereto the Tag refers.

In FoxPro this could be done with the EVALUATE function

Can this be done in VB6 ?
Maybe with the CallByName, but I can't figure how.
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

You cannot do this sort of thing in VB it just doesn't allow it. Callbyname only works with properties/methods of objects. What you are describing seems to be complicated, why do you need to assign the resnumber to a constant and then set the constant name in the .tag property. Surely keeping things simple would be better, just put the numeric value straight into the .tag property and forget about the rest of it. If you must do this though the only way to do it would be to create your own collection or array that you could use to find the numeric associated with the string.


Avatar of vindevogel
vindevogel

ASKER

I know I can do it with the numbers .... That's what I don't want.

It's far more easier to put a 'label' there instead of the number.

I want to leave this question open for other experts, so I reject your answer
Hold on can't you just use the val command

val("100,myname") should give 100
Deighton, please ....
Check the question first, think about it and don't post just for the points !!
Sorry vindevogel, TimCottee is right.
vindevogel, I think you are only going to get negative responses no matter how long you leave this question open.
Avatar of Bob Learned
I tried this, but I don't know if it will work on every machine:

I added a reference to the Micro$oft Script Control 1.0 in my project.

Private Sub Evaluate()

Dim mscEvaluator As New ScriptControl

   With mscEvaluator
      .Language = "VBScript"
      .AddCode "Const sName = " & Chr(34) & "Bob" & Chr(34)
      MsgBox .Eval("sName")
   End With
   
End Sub
TimCottee : what do you prefer : me grading you a D on this one or me waiting until I get some more answers or some suggestions ?
TimCottee : what do you prefer : me grading you a D on this one or me waiting until I get some more answers or some suggestions ?
But you were really working with Long variables, so:

Private Sub Evaluate()

Dim mscEvaluator As New ScriptControl

   With mscEvaluator
      .Language = "VBScript"
      .AddCode "Const sName = 100"
      MsgBox .Eval(vControl.Tag)
   End With
   
End Sub
But you were really working with Long variables, so:

Private Sub Evaluate()

Dim mscEvaluator As New ScriptControl

   With mscEvaluator
      .Language = "VBScript"
      .AddCode "Const sName = 100"
      MsgBox .Eval(vControl.Tag)
   End With
   
End Sub
TheLearnedOne : I don't have that in my References nor in my Toolbox ... Where did you get it ??
That it was I think I was alluding to when I said that this might not work on every system.  I don't know where I got it from, I thought it was installed with VB6, but now I clearly know that this is not.
I did a search on the Micro$oft website and came across a KB article that says that the Script control comes with the Professional and Enterprise version of VB6
Damn, have VB6 Pro, but those id---- (add the letters i o t s in a random order to complete the word) did not install it on my machine.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, it may not be the correct comment I accepted, but the series of comment by TheLearnedOne was right on !!

Thanks a lot !

You see TimCottee, leaving a question open, can give you an answer.

Caraf-g : I guess we both learned something today
As they say, you live and learn! In that respect I am grateful to thelearnedone's response as I might find this useful myself at some time.