RM032397
asked on
Evaluating a string as a variable name
Evaluating a string as a variable name
My program will replace some strings in text source files of a scripted program in order to customise the scripts.
Here is a sample of text :
var PROJECT=/*Name_Of_Project* /="Default Project Name";
The sequence "/*Name_Of_Project*/" is a comment which is also acting as my target for string search, which is not the problem.
If the string "Name_Of_Project" is padded up
"/*" & "Name_Of_Project" & "*/"
it will form the string to search for.
But if I also have declared in the VBasic
Public Name_Of_Project As String
QUESTION
How to take a parameter inside my function that has been given the string value
"Name_Of_Project" and make it give me the value of the Public String Variable
Name_Of_Project
For example in JavaScript
var X="Apple";
var Y = "X"
The expression eval(Y) will return the string "Apple".
Is there any equivalent in VBasic?
As I have a number of such strings and variables planned it will make the code more compact if I was able to use such a device.
My program will replace some strings in text source files of a scripted program in order to customise the scripts.
Here is a sample of text :
var PROJECT=/*Name_Of_Project*
The sequence "/*Name_Of_Project*/" is a comment which is also acting as my target for string search, which is not the problem.
If the string "Name_Of_Project" is padded up
"/*" & "Name_Of_Project" & "*/"
it will form the string to search for.
But if I also have declared in the VBasic
Public Name_Of_Project As String
QUESTION
How to take a parameter inside my function that has been given the string value
"Name_Of_Project" and make it give me the value of the Public String Variable
Name_Of_Project
For example in JavaScript
var X="Apple";
var Y = "X"
The expression eval(Y) will return the string "Apple".
Is there any equivalent in VBasic?
As I have a number of such strings and variables planned it will make the code more compact if I was able to use such a device.
ASKER
I regret to say that I find this answer very unclear.
OK, lets put in some clarity. I meant writing your own COM class implementing a collection of parameters.
Here Is full source code:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ParamCollection"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Collection" ,"T"
Attribute VB_Ext_KEY = "Member0" ,"T"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
Private mCol As Collection
Public Sub Add(sKey As String, ByVal vData As String)
mCol.Add vData, sKey
End Sub
Public Property Get Item(vntIndexKey As Variant) As String
Attribute Item.VB_UserMemId = 0
Item = mCol(vntIndexKey)
End Property
Public Property Let Item(vntIndexKey As Variant, ByVal vData As String)
On Error GoTo Item_Error
mCol.Remove vntIndexKey
mCol.Add vData, CStr(vntIndexKey)
Item_Error:
End Property
Public Property Get Count() As Long
Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub
Here Is full source code:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ParamCollection"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Collection" ,"T"
Attribute VB_Ext_KEY = "Member0" ,"T"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
Private mCol As Collection
Public Sub Add(sKey As String, ByVal vData As String)
mCol.Add vData, sKey
End Sub
Public Property Get Item(vntIndexKey As Variant) As String
Attribute Item.VB_UserMemId = 0
Item = mCol(vntIndexKey)
End Property
Public Property Let Item(vntIndexKey As Variant, ByVal vData As String)
On Error GoTo Item_Error
mCol.Remove vntIndexKey
mCol.Add vData, CStr(vntIndexKey)
Item_Error:
End Property
Public Property Get Count() As Long
Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub
ASKER
Thanks for the code borov. I'll need some time to figure out how this might help me because the fact is it is beyond me at least until I study it in detail.
seahpc: The system is showing an error in giving answer-2 to this question which I assume is your contribution. I cannot respond to your answer except to tell you this, as I cannot see your answer, only that in #1 above it says "Grade seahpc's answer:" !
seahpc: The system is showing an error in giving answer-2 to this question which I assume is your contribution. I cannot respond to your answer except to tell you this, as I cannot see your answer, only that in #1 above it says "Grade seahpc's answer:" !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. This is not what I had hoped for but it will have to do.
Thanks to both respondents for their trouble which is appreciated.
Thanks to both respondents for their trouble which is appreciated.
Dim Vars as new ParamsCollection
.....
Vars("Name_Of_Project") = Variable_Of_Any_Kind
....
INeedIt = Vars("Name_Of_Project")