-
Call shared method without class instantiation?
I'm reading through Keith Franklin's "VB.NET for Developers". Good book so
far, fast read.
On pages 51/52 he talks about Shared methods, and how you can call them
without instantiating the class. In his example, he has a method called
AddEmployee in an Employee class. He indicates that this:
Dim oEmp as new Employee
oEmp.AddEmployee('...params here)
and
Employee.AddEmployee('...params here)
Are equivalent, when the Shared keyword is used in the procedure declaration
for the AddEmployee method. This _seems_ similar to the way VB currently
handles forms.
My question is, how is this working behind the scenes?
Is VB creating an instance of the class Employee, calling the method
AddEmployee, then destroying that instance all in one step?
If so, do the constructor/destructor methods get called (New and Finalize I
believe, formerly Class_Initialize and Class_Terminate in VB6).
Thanks,
Robert
-
Re: Call shared method without class instantiation?
An object is not instantiated. It is much more like calling a procedure in
a module. It just executes the code. I think you will find that you can't
make a method shared if it requires any resources that aren't also shared.
-
Re: Call shared method without class instantiation?
I have tested myself:
Class Example
Public Sub New()
MsgBox("Construtor has been called!")
End Sub
Shared Sub Messsage()
MsgBox("Hi from message!")
End Sub
End Class
I then invoke "message" and the construtor is not called. I "think" VB just
create an instance itself, ignoring the construtor / destrutor, just to call
the method. Although i am not aware about more technical details.
Joćo Ferreira
-
Re: Call shared method without class instantiation?
"Robert C. Cain" <robert.cain@comsys.com> wrote:
>I'm reading through Keith Franklin's "VB.NET for Developers". Good book
so
>far, fast read.
>
>On pages 51/52 he talks about Shared methods, and how you can call them
>without instantiating the class. In his example, he has a method called
>AddEmployee in an Employee class. He indicates that this:
>
>Dim oEmp as new Employee
>oEmp.AddEmployee('...params here)
>
>and
>
>Employee.AddEmployee('...params here)
>
>Are equivalent, when the Shared keyword is used in the procedure declaration
>for the AddEmployee method. This _seems_ similar to the way VB currently
>handles forms.
>
>My question is, how is this working behind the scenes?
>
>Is VB creating an instance of the class Employee, calling the method
>AddEmployee, then destroying that instance all in one step?
>
>If so, do the constructor/destructor methods get called (New and Finalize
I
>believe, formerly Class_Initialize and Class_Terminate in VB6).
>
>Thanks,
>
> Robert
>
>
When calling a 'Shared' method you can call it with either the class name
or with a variable declared as the class name. Calling a shared method does
not instantiate an instance of the class but does instantiate 'THE' instance
of the class object. For that matter if you declare a constructor on your
class with the shared keyword i.e. 'Shared New()' this will give ou a 'Class'
constructor as opposed to an object constructor 'Public New()'. To test this
add a shared method and access it through a variable declared as the class
or through the class name - you will see the class constructor executed even
tough there are as yet no instances of the class. In fact the class constructor
will be called only once in the program. The class constructor is also called
before the instance constructor the first time you instantiate an object
of the class.
There is NO EQUIVALENT to a class constructor(Shared New) or a class method(Shared
OpName) in VB6. The class_initialize was simply a poor imitation of the true
object constructor (which is parametised, can be overloaded etc etc) in VB.Net.
All Class Objects are Singletons. I hope this is clear (although I know it
is not - no calls from APress just yet ....)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|