CallByName - Referenced by a Variable.

  • Thread starter Thread starter runningdog
  • Start date Start date
R

runningdog

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
 
Steve,
1. How do I code an ObjectRef so that it can be stored in a variable.
"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)
2. Can I reference methods in a module with a CallByName or similar
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
 
runningdog said:
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.
Got this one working
 
Jay said:
Steve,


"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)



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
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)
 
Steve,
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)
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
 
Jay said:
Steve,


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.
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
Or is your second post saying you figured the above out? Yes, Thanks for the input.

Hope this helps
Jay

Jay B. Harlow [MVP - Outlook] wrote:

Steve,


1. How do I code an ObjectRef so that it can be stored in a variable.

"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)



2. Can I reference methods in a module with a CallByName or similar

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

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)

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
 
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.
Project A.
Public Interface IMyObject
Sub MyMethod
End Interface


Public o As IMyObject
... o.MyMethod()


Project B.
A.o = MyObject ' new Whatever()
...
Public Class Whatever
Implements A.IMyObject
Public Sub MyMethod Implements A.IMyObject.MyMethod
...
End Sub

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


runningdog said:
Steve,


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.
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
Or is your second post saying you figured the above out? Yes, Thanks for the input.

Hope this helps
Jay

Jay B. Harlow [MVP - Outlook] wrote:


Steve,


1. How do I code an ObjectRef so that it can be stored in a variable.

"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)



2. Can I reference methods in a module with a CallByName or similar

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


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)




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
 
Jay,

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

Steve
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.

Project A.

Public Interface IMyObject
Sub MyMethod
End Interface


Public o As IMyObject
...
o.MyMethod()



Project B.
A.o = MyObject ' new Whatever()
...

Public Class Whatever
Implements A.IMyObject
Public Sub MyMethod Implements A.IMyObject.MyMethod
...
End Sub


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


Jay B. Harlow [MVP - Outlook] wrote:

Steve,


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)

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.

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
Or is your second post saying you figured the above out?

Yes, Thanks for the input.
Hope this helps
Jay



Jay B. Harlow [MVP - Outlook] wrote:



Steve,



1. How do I code an ObjectRef so that it can be stored in a variable.

"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)




2. Can I reference methods in a module with a CallByName or similar

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


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)






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
 
Back
Top