Solved

Set object whit a variable name

Posted on 2001-06-04
2
144 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

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 While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

864 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

21 Experts available now in Live!

Get 1:1 Help Now