I.T. Discussion Community!
-Collapse +Expand
VB.Net
Search VB.Net Group:

Advanced
-Collapse +Expand VB.Net To/From
To/FromCODEGuides
-Collapse +Expand VB.Net Store
PRESTWOODSTORE

Prestwood eMagazine

February Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingVB.NetOOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
VB.Net OOP:
VB.Net Constructors (New)
 
Posted 10 years ago on 1/18/2009
VB.Net Code Snippet:
 A flashcard from our VB.Net Flashcards Library
 A code snippet from our VB.Net Code Snippets Page

KB101820



General Info: Class Constructor

Constructors are called when you instantiate an object from a class. This is where you can initialize variables and put code you wish executed each time the class is created. When you initially set the member fields and properties of an object, you are initializing the state of the object. The state of an object is the values of all it's member fields and properties at a given time.

Languages Focus: Constructor

What is the syntax? Can you overload constructors? Is a special method name reserved for constructors?

VB.Net Constructors

In VB.Net, a constructor is called whenever a class or struct is created. A constructor is a sub named New. You can overload the constructor simply by adding two or more New subs with various parameters (overloaded constructors).

If you do not create a constructor, VB.Net will create an implicit constructor that initializes all member fields to their default values.

Constructors can execute at two different times. Static constructors are executed by the CLR before any objects are instantiated. Regular constructors are executed when you create an object.

You can invoke the parent constructor with MyBase.New.

Syntax Example:
Public Class Cyborg
Public CyborgName As String
  
  Public Sub New(ByVal pName As String)
CyborgName = pName
End Sub
End Class

Working WinForms Example

The following example demonstrates using a constructor to initialize a public member field.

Create a form and place a button on it and alter the code as follows:

Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyRobot As New Cyborg("Cameron")
  
    MessageBox.Show("My robot's name is " & MyRobot.CyborgName & ".")
End Sub
End Class
  
Public Class Cyborg
Public CyborgName As String
  
  Public Sub New(ByVal pName As String)
CyborgName = pName
End Sub
End Class

Overloading Constructors

Here is a working example of overloading a constructor. In this example, we overload our New() constructor so you can either pass in a name or set the name after creation.

 
Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
 
    Dim MyRobot1 As New Cyborg()
MyRobot1.CyborgName = "John"
MessageBox.Show("My robot's name is " & MyRobot1.CyborgName & ".")
 
    Dim MyRobot2 As New Cyborg("Cameron")
MessageBox.Show("My robot's name is " & MyRobot2.CyborgName & ".")
  End Sub
End Class
 
Public Class Cyborg
Public CyborgName As String
 
  Public Sub New()
End Sub
 
  Public Sub New(ByVal pName As String)
CyborgName = pName
End Sub

End Class

More Info

Definition:  Class Constructor

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1215A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

2 Intermediate Level Questions

Question #1: Multiple Choice

The method name of a constructor is?

Answer:
1. 

Class_Initialize

2. 

~ + class name, i.e. ~Person

3. 

Create

4. 

New

5. 

Same name as the class.

Question #2: Yes or No?

If you do not create a developer defined constructor, does VB.Net create an implicit constructor and initialize all member fields to their default values?

Answer:
  • Yes
  • No

  •  KB Article #101820 Counter
    15166
    Since 1/18/2009
    -
       Contact Us!
     
    PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

    2,299 People Online Now!!  
    Sign In to see who's online now!  Not a member? Join now. It's free!
    Show more stats...


    ©1995-2019 PrestwoodBoards  [Security & Privacy]
    Professional IT Services: Coding | Websites | Computer Tech