Solved

Evaluating a string as a variable name

Posted on 1998-04-15
6
342 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:RM032397
  • 3
  • 2
6 Comments
 

Expert Comment

by:borov
ID: 1461313
There is no such thing in VB, but you can easily do it - just write your own collection of parameters (using std collection is not very convinient - you must Remove/Add item to change it value). I have no sources under hand, but it's easy task - just override Item propery and make Set property, too. Using built-in VB5 class builder, its relatively easy task. Than your code will look like:

Dim Vars as new ParamsCollection
.....
Vars("Name_Of_Project") = Variable_Of_Any_Kind
....
INeedIt = Vars("Name_Of_Project")

0
 
LVL 3

Author Comment

by:RM032397
ID: 1461314
I regret to say that I find this answer very unclear.
0
 

Expert Comment

by:borov
ID: 1461315
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:RM032397
ID: 1461316
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:" !

0
 
LVL 45

Accepted Solution

by:
aikimark earned 100 total points
ID: 1461317
I have an interesting approach to your problem that you might give a try.  Using DAO, create a tabledef object with one column for each (public) variable you wish to substitute this way.  You will only need one row in this table to contain the values for this variable.
Once this is done, you can do the kind of substitution you desire by referencing
databasevar.tabledef("nameoftable").fields(strVar_you_read_from_file).value

If you do this a lot, you will want to preassign a recordset variable to the tabledef to speed-up the process.
0
 
LVL 3

Author Comment

by:RM032397
ID: 1461318
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now