Solved

Set object whit a variable name

Posted on 2001-06-04
2
143 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

772 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

13 Experts available now in Live!

Get 1:1 Help Now