24
Working With Objects Tonga Institute of Higher Education

Working With Objects

  • Upload
    cyrah

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Working With Objects

Working With Objects

Tonga Institute of Higher Education

Page 2: Working With Objects

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)

Page 3: Working With Objects

Advantages of Object Oriented Programming

Code is easier to maintain Code is more readable Encapsulation

Show what the object can do This is normally what we want to know

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 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

Page 4: Working With Objects

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(…)

Page 5: Working With Objects

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

Page 6: Working With Objects

Using Object Variables

2 Steps to using variables1. Declare the variable

2. Instantiate the variable / Initialize the variable

Page 7: Working With Objects

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. Name of the variable2. Type of the variable (What kind of variable you have)

String StringBuilder TextBox

TypeName

Page 8: Working With Objects

Declaring Object Variables – 2

Use a name that is easy to remember. Do not use x, y, z

Begin each separate word in a name with a capital letter Examples

FirstName CustomerID

This works exactly the same for primitive variables!

Page 9: Working With Objects

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

New Keyword Type of Object

Sometimes extra data is required

Page 10: Working With Objects

Declaring and Initializing Object Variables in 1 line

You can declare and initialize a variable in 1 line.

Page 11: Working With Objects

Demonstration

Declaring, Instantiating and Initializing Objects

Page 12: Working With 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

Page 13: Working With Objects

Method Inputs - 1

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()

MethodInput Output

Page 14: Working With Objects

Method Inputs - 2

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.

MethodInput Output

Click on link to get more informationMethod Name

Parameters

Page 15: Working With Objects

Method Outputs

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

MethodInput Output

OutputInformation

Page 16: Working With Objects

Functions vs. Subroutines

Subroutine – A method that does not return anything

Function – A method that returns something

Page 17: Working With Objects

Demonstration

Methods

Page 18: Working With Objects

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 can find a list of constructors in the MSDN Documentation.

You can also find a list of constructors using the IntelliSense capability.

Dim x as StringBuilder = new StringBuilder

Dim y as StringBuilder = new StringBuilder(“hello”)Dim z as StringBuilder = new StringBuilder(6)

You don’t need parenthesisif you are not passing parameters

Page 19: Working With Objects

Demonstration

Constructors

Page 20: Working With Objects

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.

Page 21: Working With Objects

Demonstration

Attributes / Fields

Page 22: Working With Objects

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.

Page 23: Working With Objects

Demonstration

Overloaded methods

Page 24: Working With Objects

Members

Member – An attribute/field or method. Sometimes used to refer to the both as a

whole.