X hits on this document

PDF document

“Beginning Visual C# 2005 Express Edition Video Series” - page 28 / 73





28 / 73

Introducing Constructors

First of all, classes have Constructors, which is a special method that allows you to initialize your fields, or whatever else you may want to do when your object is first created. In Visual Basic.NET, you create a Constructor using the New statement:

Visual Basic:

Public Sub New() m_make = "Unknown" m_model = "Unknown" m_elapsedMileage = 0 End Sub

Here we've set our private fields to default values. The Constructor gets called whenever the New keyword is used in association with our class, like so:

Visual Basic:

myCar = New Car

Also, the constructor is ALWAYS the first code to execute within your class, however having a Constructor in your class is optional. While Constructors are not required, it is a good idea to use constructors to initialize the private property (private member) values of your new object.

I’ve used the term “initialize” several times; let me explain what I mean. Initialization is the process of taking steps to ensure that your object will function properly by the time the object is used in an application. That means different things based on how you design your object. For example, you may choose to create a connection to a database, or create instances of child objects, or set variables (like private fields) to default values, etc. The

Supplemental Readings for the Express Edition Videos Copyright © 2005 LearnVisualStudio.NET


Document info
Document views222
Page views222
Page last viewedTue Jan 17 05:21:51 UTC 2017