Solved

Declare variable from a string

Posted on 2002-07-30
4
205 Views
Last Modified: 2010-05-01
In a function, i receive a string. In this function, i need to declare a variable who will get the name of what this string contain. For example, if i receive a string who contain "testing", i will have to declare a variable who will be named testing like if i done dim testing. How can i do that, it's so easy in asp or php.

Thankx
0
Comment
Question by:mammouth
4 Comments
 
LVL 3

Expert Comment

by:RodStephens
ID: 7187939
You cannot really do that in VB using one of the "simple" data types like String. However, you can create a variable of a more complicated data type.

For example, you can use New to create a new instance of an object. You could make a class called DynamicVariable and give it Name and Value properties. Then when you need to make a new "testing" variable, you can do something like this:

    Dim new_variable As DynamicVariable

    Set new_variable = New DynamicVariable
    new_variable.Name = "testing"
    new_variable.Value = "whatever"

Store the variables in a Collection so you can get them and their values later.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7187942
This can't be done. If you think about it, VB is compiled and variables needs to be declared before the compilation occurs.

If you tell us what is your need, maybe we will be able to suggest you a workaround.
0
 
LVL 1

Author Comment

by:mammouth
ID: 7188001
Here is an example of my code

I have 5 checkbox chk0, chk1, chk2, chk3, chk4 each one is an array of a different size. ex.

chk0(5)
chk1(12)
chk2(7)
chk3(3)
chk4(22)

These checkbox are on cell of different grid on sstab.

For now i have this code

select case sstab.tab
    case 0:
        for i=0 to ubound(chk0)
             chk0(i).value=1
        next
    case1:
        for i=0 to ubound(chk1)
             chk1(i).value=1
        next
end select

i want to be able to put this with something like that

myvar="chk" & sstab.tab
for i=0 to ubound(myvar)
   myvar(i).value=1
next

It is possible, When i try the answer from RodStephens, i get an error.

thankx
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 7188018
try this:

myvar="chk" & sstab.tab
for i=0 to ubound(controls(myvar))
  controls(myvar)(i).value=1
next

CHeers
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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now