Transcript Loops

Working With
Objects
Tonga Institute of Higher
Education
Introduction





Visual Basic .NET is an object-oriented language
The building block of an object-oriented
language is an object.
Object - A self-contained entity that contains data
and procedures to manipulate the data.
An object is like a tool that we can use to do
things.
You can find a list of pre-built objects in the
MSDN Documentation. (www.msdn.com)
Advantages of Object Oriented
Programming

Code is easier to maintain


Code is more readable
Encapsulation

Show what the object can do


Hide how the object does it



This can be very complicated
We often don’t care how it is done
Code is easier to reuse



This is normally what we want to know
A piece of code, once written, should not be thrown away. It
is best to reuse the code in other programs.
Example: Millions of people use MessageBox.Show(...).
But it was only written once.
Code development is more efficient

You don’t code the same thing over and over again
Objects

Objects are like variables with extra
functionality.

Look at MSDN documentation to find
functionality.

Examples:


String
 String.Replace(…)
 String.PadLeft(…)
TextBox
 TextBox.Text(…)
 TextBox.MaxLength(…)
Classes vs. Objects




Object - A self-contained entity that contains
data and procedures to manipulate the data.
Class - The blue print or design for creating an
object.
Instantiate – The act of creating an object from a
class
Instance – An instantiated class/object
Using Object Variables

2 Steps to using variables
1. Declare
the variable
2. Instantiate the variable / Initialize the variable
Declaring Object Variables – 1

Declare the variable – Tell the computer to reserve a space in
memory for the variable.

You need to tell the computer 2 things:
1.
2.
Name of the variable
Type of the variable (What kind of variable you have)



String
StringBuilder
TextBox
Name
Type
Declaring Object Variables – 2

Use a name that is easy to remember.



Begin each separate word in a name with a capital letter
Examples



Do not use x, y, z
FirstName
CustomerID
This works exactly the same for primitive variables!
Instantiating Object Variables /
Initializing Object Variables



Initialize the variable – Assign an initial value to a
variable
Instantiate – The act of creating an object from a class
Use the new keyword
Sometimes
extra data is
required
New Keyword
Type of Object
Declaring and Initializing Object
Variables in 1 line

You can declare and initialize a variable in 1 line.
Demonstration
Declaring, Instantiating and
Initializing Objects
Methods

Methods - Pieces of code that perform a single
function
 Use

dot notation to access it
Example: <Object name>.<method name>(<parameters>)
 You
can find a list of methods in the MSDN
Documentation.
 You can also find a list of methods using the
IntelliSense capability.

Calling a Method – The act of using a method
Method Inputs - 1
Input

Method
Output
Some methods take inputs
Parameter/Arguments – A piece of information that
provides additional information to the method as to
how it should behave.
 Parameters should be in the parenthesis next to the
method name
 The order they are passed is important
 Values are separated by commas
 If you are not passing any parameters, you may or
may not use (). It is up to you.



Example: String.Trim
Example: String.Trim()
Method Inputs - 2
Input

Method
Output
Some methods take inputs
 You can find a list of parameters in the MSDN
documentation.
 You can find a list of parameters using the IntelliSense
capability.
Method Name
Click on link to get more information
Parameters
Method Outputs
Input

Method
Output
Some methods return outputs

When something is returned, it may or may not be used.





The programmer chooses what to do with the data returned.
Only one thing may be returned.
If something is coming back, we can see “As <object>” at the end of
the method in IntellliSense
If nothing is coming back, you will not see “As <object>” at the end of
a method in IntellliSense
Use popup windows while coding to see what is being returned
Output
Information
Functions vs. Subroutines
Subroutine – A method that does not
return anything
 Function – A method that returns
something

Demonstration
Methods
Constructor

Constructor – A method that is automatically executed when an
object is created.


This allows you to set initial values for the object.
Many objects have multiple constructors. (They are overloaded)
You don’t need parenthesis
if you are not passing parameters
Dim x as StringBuilder = new StringBuilder
Dim y as StringBuilder = new StringBuilder(“hello”)
Dim z as StringBuilder = new StringBuilder(6)


You can find a list of constructors in the MSDN
Documentation.
You can also find a list of constructors using the
IntelliSense capability.
Demonstration
Constructors
Attributes / Fields / Properties

Attributes / Fields / Property – A variable that a
class allows others to see

Use dot notation to access it



Example: <Object name>.<field name>
You can find a list of attributes / fields /
properties in the MSDN Documentation.
You can also find a list of attributes / fields /
properties using the IntelliSense capability.
Demonstration
Attributes / Fields
Method Overloading





If two methods do the same thing, they should have the same name
Overloading - Having multiple methods with the same name but
different parameters
The correct method to use is determined by matching up the number
and type of arguments.
Therefore, you can’t have 2 methods with the same name and same
number & type of arguments.
Without overloading, we would have to remember more function
names. That would make code more complicated.
Demonstration
Overloaded methods
Members
Member – An attribute/field or method.
 Sometimes used to refer to the both as a
whole.
