Groups | Blog | Home
all groups > vb.net > march 2004 >

vb.net : CallByName - Referenced by a Variable.



Jay B. Harlow [MVP - Outlook]
3/16/2004 7:46:02 AM
Steve,
[quoted text, click to view]
"ObjectRef" is the name of the parameter, the type of the parameter is
"Object". Use any Object variable as the parameter. Remember that all types
inherit from Object, so you can use any variable as the first parameter.

Dim i As Integer = 100
Dim o As Object
o = CallByName(i, "ToString", CallType.Method)

[quoted text, click to view]
I don't see that Modules or Shared methods are supported, as they do not
have an instance of an Object associated with them. The ObjectRef parameter
is required, you get an exception if you attempt to pass Nothing. This makes
sense as I am certain that it is using Reflection to check the object to see
if it has the method (without the object it would not know if it had the
method or not).

Also remember that multiple Modules can have a method with the same name in
them.

Hope this helps
Jay

[quoted text, click to view]

Jay B. Harlow [MVP - Outlook]
3/16/2004 9:25:40 AM
Steve,
[quoted text, click to view]
I'm not following you?

MyObject needs to be an object of a type (class or structure) that has a
method (sub or function) called MyMethod, if it doesn't it will fail. Also
you will need to be certain to match the number & types of the parameters.

Or is your second post saying you figured the above out?

Hope this helps
Jay

[quoted text, click to view]

Jay B. Harlow [MVP - Outlook]
3/16/2004 3:55:27 PM
Steve,
Rather then using CallByName, which is a form of Late Binding, consider
using an Interface which is a form of Early Binding.

Early Binding will perform better then Late Binding.

[quoted text, click to view]
Public Interface IMyObject
Sub MyMethod
End Interface


Public o As IMyObject
[quoted text, click to view]
o.MyMethod()


[quoted text, click to view]
Public Class Whatever
Implements A.IMyObject
[quoted text, click to view]

Of course knowing how CallByName is also helpful, especially when the method
name varies and may be coming from a database or XML file...

Hope this helps
Jay


[quoted text, click to view]

runningdog
3/16/2004 11:23:04 PM
I'm having difficulty with CallByName.
Syntax ask for an ObjectRef as Object.

1. How do I code an ObjectRef so that it can be stored in a variable.
2. Can I reference methods in a module with a CallByName or similar


Thanks in advance
Steve
runningdog
3/17/2004 12:19:21 AM


[quoted text, click to view]
Got this one working

[quoted text, click to view]
runningdog
3/17/2004 12:41:08 AM


[quoted text, click to view]
Thanks I had come to a similar conclusion. Modules just looked like a
convenient container for the project I'm working on, but I class works
nicely to.

RE: 1. I think you will find that while your analysis is ok, the example
will fail looking for Object 100.
Dim o As Object
o = MyObject ' Declared elsewhere in the solution
CallByName(o, "MyMethod", CallType.Method)

[quoted text, click to view]
runningdog
3/17/2004 8:04:22 AM

[quoted text, click to view]
Myobject is an object with a set of methods declared in another project
that will consume this object that needs to interact with Myobject. The
code looks more like.

Project A.
Public o As Object
...
CallByName(o, "MyMethod", CallType.Method)

Project B.
A.o = MyObject
...
Public Sub MyMethod
...
End Sub
[quoted text, click to view]
runningdog
3/17/2004 12:45:31 PM
Jay,

That is most helpful thanks.
I'm very new to VB and this kind of input makes the curve a lot easier.

Steve

[quoted text, click to view]
AddThis Social Bookmark Button