Group Discounts available for 3+ students and Corporate Clients

Working of Constructors in vb.net

Constructors:

It is used to initialize the members of the class

VB.Net supports two types of constructors

  • Instance constructors
  • Shared constructors

 

Instance Constructors:

It is used to initialize the instance and the shared members of the class 

 

Syntax:

[Modifier] sub new ([arg info])                                                                                                  statement(s)                                                                                                                                    – – – – – – –                                                                                                                                                – – – – – – –                                                                                                                                            End sub

Note:

  • Instance constructors will be invoked every time a new object is created for the class.
  • A class can have any number of instance constructors.
  • If a class is defined with more than one constructor then it is said to be the classes overloaded with constructors.
  • If a constructor is defined using arguments then it is said to be parameterized constructor
  • If a constructor is defined without any arguments then it is said to zero argument constructor.

Shared Constructors:

It is used to initialize the shared members of the class.

Syntax:

Shared sub new ( )                                                                                                 statement(s)                                                                                                                                    – – – – – – –                                                                                                                                            – – – – – – –                                                                                                                                            End sub

 

 

 

 

Observation:

  • Shared constructors will be invoked only once whenever the class definition is loaded.
  • A class can have maximum of only one shared constructors.
  • Shared constructors can’t accept any arguments.

Project [menu] à Add new item à Module

Name: Constructors ‘‘Demo

‘‘‘Demo on constructors . . . . .

Imports system

Module constructors ‘‘Demo

Class Test

Public i as integer

Public shared s as integer

Sub new( ) ‘Instance constructor’

P = 100

S = 1000

End sub

Shared sub new( ) ‘Shared constructor’

S = 500

P = 500 error

End sub

End class

Sub main ( )

Console.WriteLine (“value of s before object creation: {0}”, Test.s)

Dim t As new Test

Console.WriteLine (“Value of t and s” after object creation: {0}, {1}”,                           t.p, Test.s)

                        End sub

End module

 

 

Screenshot_25

 

Observation:

  • Whenever the value for the shared members has to be initialized before creating an object or if a constant value has to be assigned for the shared member then shared constructors should be used to initialize the shared members.
  • Whenever the value for the shared member has to be reinitialized with a new value for every new instance created for a class then instance constructor should be used to initialize the shared members.

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Working of Constructors in vb.net"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please Enter Your Details and Query.
    Three + 6