Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Set object whit a variable name

Posted on 2001-06-04
2
Medium Priority
?
153 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 225 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

926 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