Link to home
Start Free TrialLog in
Avatar of gem56
gem56

asked on

How to formulate a pointer to an object

Hi experts,
I'd like to access an object whose reference is based on contents of one or more strings. To help explain what I mean I'll give you an example.

I have a string variable called 'ObjectName', that will contain the name of the object that I want to access, and a variable called 'PropertyName, that will contain the name of the property within an object. What I'd like to do is basically combine the contents of those two strings to form a reference/pointer to the actual object property, as per below:

Dim ObjectName as String
Dim PropertyName1 as String
Dim PropertyName2 as String

     pointer?(ObjectName & PropertyName1) = "12345"
     pointer?(ObjectName & PropertyName2) = "ABC12345"
                            or
     set pointer?(ObjectName & PropertyName1) = xxxxxx
     set pointer?(ObjectName & PropertyName2) = xxxxxx


The above syntax examples are incorrect but serve the purpose of explaining (I hope) what I'm trying to achieve.

Can it be done?

/Michael
Avatar of JR2003
JR2003

How would you know which instance of the object to access if you only have a string with its name in?

Have you looked at the 'CallByName' function to see if that meets your requirements?
Put htis code in form. This will set text1 property of text1 object

Private Sub Command1_Click()
Dim myObj As Object
    Set obj = Me.Controls("Text1")
    If obj Is Nothing Then
        MsgBox "Can not find control"
    Else
        CallByName obj, "Text", VbLet, "Some text"
    End If
End Sub
Avatar of gem56

ASKER

Hi mladenovicz,
Thanks for your response however I know the syntax for Form objects, which is as per your feedback, but what I was referring to is class objects. I think that JR2003 may be on teh right track.
/Michael
I think that you will have to store objects in collection in order to access them by name.
However, CallByname is the way to set/get/let property or call method of object
Avatar of gem56

ASKER

Hi JR2003,
I just had a quick look at help for your suggestiopn ('CallByName') and I think that you may be right however I don't have any examples so I'll have to try it out. If my initial understanding is right would I use that function in the following way for my example above?

Dim ObjectName as String
Dim PropertyName1 as String
Dim PropertyName2 as String

     CallByName(ObjectName, PropertyName1,vbLet,"12345")
     CallByName(ObjectName, PropertyName2,vbLet,"ABC12345")
                            or
     CallByName(ObjectName, PropertyName1,vbSet,xxxxxx)      ' where xxxxxxx is an object
     CallByName(ObjectName, PropertyName2,vbSet,xxxxxx)

Cheers,
     Michael
ASKER CERTIFIED SOLUTION
Avatar of JR2003
JR2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial