Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Saturday, May 12, 2012

Instance Members vs Shared Members in VB.Net

Here in this article, we will discuss about Instance Members and Shared Members in VB.Net. Also you can check SQL Server Articles.

Members of a class in VB.Net are classified into Instance members and Shared members.
Members of the class that can be acessed only by creating an instance for the class called it as a instance members and the members of the class that can be acessed directly by using class name without creating instance are called as Shared Members.
To declare the members as Shared member use the keyword Shared while declaring that member.

Shared member cannot be acessed using an instance and Instance member cannot be acessed by using class name.
For the instance members memory will be allocated when an instance is created by using the keyword new.
But for shared members memory will be allocated while program that contains the class loaded into memory.

Shared Fields:
You can declare the fields avaible in the class as shared, But when the fields are declared as shared then those fields will exist only one copy in memory for any number of instance created for the class.

Main purpose of th Shared field is to store the data i.e to be shared by all instance of the class.

For Example, When you want to maintain a count of how many instances are created for the class upto now then you must create the field as shared.

Shared Methods:

Methods in a class can be declared as shared, but shared methods can acess only other Shared members available in the class and they cannot acess instance members of the class.

The concept in C#.Net i.e equivalent to shared members in VB.Net is Static members. Equivalent to Shared fields  C#.Net  provides static fields and eqivalent to Shared fields  C#.Net provides static fields and equivalent to Shared methods  C#.Net  provides static methods. also provides static classes when every member in the class is static then class can be declared as static.
Class can be declared as static so that creating an instance for the class will not be allowed indicating every member in the class is static. But doesnot provide shared classes.

VB.Net provides the keyword static. But in VB.Net static keyword is used like a storage class in C. It can be used in VB.Net only for declaring local variables local variables of methods.

0 on: "Instance Members vs Shared Members in VB.Net"