Using CallByName with nested objects

Using CallByName with nested objects

Sometimes Microsoft documentation can be, well lacking is a kind word, and one is reluctant to call tech support when they smoke your credit card first, and ask questions later.

My problem was that the CallByName procedure was refusing to call nested lasses. To see what I mean, consider that if your class exposes an X public variable, you can set it to zero from within the class itself using this code

CallByName me, "X", vbLet, 0

But, what happens if you have an array of objects, say Data(), and you want to set the X property for any object in the array, given its index? The first attempt I made was:

CallByName Me, "Data(index).X", vbLet, 0

which tesults in the error “Object doesn’t support this property or method”.

Hmmmm, seems like the ‘C’ VB programmers were not willing to go the extra mile for us. I was desperate to use this function so I persisted until I found the following hack:

CallByName CallByName(Me, "Data", vbGet, index), "X", vbLet, 0

The second CallByname creates an object reference (pointer) direct to the nested class object, thus we are not using any nested calls in the function. I see no reason why you couldn’t keep nesting this hack if you have further nested class objects. Just remember VB is using late binding in all these calls!

See also  Why ChatGPT Is So Important Today

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist