Solved

How to formulate a pointer to an object

Posted on 2004-09-03
6
206 Views
Last Modified: 2012-03-15
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
0
Comment
Question by:gem56
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11973502
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?
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973520
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
0
 

Author Comment

by:gem56
ID: 11974891
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
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 8

Expert Comment

by:mladenovicz
ID: 11974932
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
0
 

Author Comment

by:gem56
ID: 11975045
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
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 250 total points
ID: 11975214
gem56,

Here's the help for the CallByName function.
ObjectName is the actual reference to the object not a string containing the name of the object.
So where you would normally hard-code say:

You could code this:
s = MyObject.StartTime

as:

Dim sFuncName as string
sFuncName = "StartTime"
s = CallByName(MyObject, sFuncName, vbGet)

It gives you flexibility to code function calls in a "soft" way.

JR



CallByName Function
     
Executes a method of an object, or sets or returns a property of an object.

Syntax

CallByName(object, procname, calltype,[args()])

The CallByName function syntax has these named arguments:

Part Description
object Required; Variant (Object). The name of the object on which the function will be executed.

procname Required; Variant (String). A string expression containing the name of a property or method of the object.

calltype Required; Constant. A constant of type vbCallType representing the type of procedure being called.

args() Optional: Variant (Array).


Remarks

The CallByName function is used to get or set a property, or invoke a method at run time using a string name.

In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:

CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100

0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

691 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