Solved

Set object whit a variable name

Posted on 2001-06-04
2
145 Views
Last Modified: 2010-05-02
I want to make a function who get a param name and will create a object whit this name ex.:

'my sub:
Sub CreateObj(UserId as integer)
   Dim "UserId" & UserId as MyObject
   SET "UserId" & UserId = New MyObject
End Sub

if i send 10 to the function this will make the same thing than:

Dim UserId10 as MyObject
SET UserId10 = New MyObject

How can i make my sub for it works ?
0
Comment
Question by:mammouth
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 75 total points
ID: 6152145
You cannot do this in quite the way you describe. You can however declare an array of objects and reference them like that:

Dim aryMyObjects(10) As MyObject

Sub CreateObj(UserID As Integer)
  Set aryMyObjects(UserID) = New MyObject
End Sub

Or with a collection

Dim colMyObjects As Collection

Private Sub Form_Load()
  Set colMyObjects = New Collection
End Sub

Sub CreateObject(UserID As Integer)
  Dim NewObject As MyObject
  Set NewObject = New MyObjects
  colMyObjects.Add NewObject,UserID
End Sub

You can then refer to the created object using

colMyObjects(UserID).Property
0
 
LVL 2

Expert Comment

by:WalterM
ID: 6156267
Why would you want to do this?

Names that you give to variables are used only from within your source code, not in the compiled binary (except for debugging information). Dynamically generating variable names is only useful for Add-Ins and code generators.

If you need to be able to discern between objects, I suggest you add a property to your class that stores the User ID as mentioned in your example. Assuming you call the property "UserID", then you can code your creation function as follows:

   Function CreateObj(UserId as integer) As MyObject
      Set CreateObj = New MyObject
      CreateObj.UserID = UserID
   End Sub

Use the function like this:

   Dim O As MyObject
   Set O = CreateObj(10)

To inspect the User ID of any MyObject instance, use the property as in:

   If O.UserID = 10 Then ...

Optionally, you could store the created objects in a collection:

   Dim MyObjects As Collection

   Function CreateObj(UserID As Integer) As MyObject
      Set CreateObj = New MyObject
      CreateObj.UserID = UserID
      MyObjects.Add CreateObj, CStr(UserID)
   End Sub

so you can enumerate them:

   Dim O As MyObject
   For Each O in MyObjects
      Debug.Print O.UserID
   Next O

and retrieve one by it's UserID:

   Dim O As MyObject
   Set O = MyObjects.Item("10")

This way, you don't need to declare a variable for each object instance you're application requires, because you can retrieve the required instance from the collection into a temporary variable of the type MyObject.

Michel
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using "ScreenUpdating" 6 63
Hide vba in gp 7 93
MsgBox 4 59
Publisher:   Unknown     VB.exe Application 1 17
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

815 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

12 Experts available now in Live!

Get 1:1 Help Now