CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011.

Slides:



Advertisements
Similar presentations
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Advertisements

12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Classes and Objects Systems Programming.
Summary of text to be covered Create your own class Create and use objects of your own class Control access to object instance variables Use keyword private.
C++ data types. Structs vs. Classes C++ Classes.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
ASP.NET Programming with C# and SQL Server First Edition
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Chapter 8 More Object Concepts
Module 7: Object-Oriented Programming in Visual Basic .NET
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Chapter 10 Classes and Objects: A Deeper Look Visual C# 2010 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
1 Working with Objects. 2 Defining a Class in VB.NET A class is a user-defined data type You can declare it as part of a module, but usually in a separate.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Object-Oriented Programming: Classes and Objects.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CompSci 230 S Programming Techniques
Object-Oriented Programming: Classes and Objects
More About Objects and Methods
Microsoft Visual Basic 2005: Reloaded Second Edition
Creating Your OwnClasses
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance
Object Based Programming
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
More on Classes and Objects
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects – Exercises UTPA – Fall 2012 This set of slides is revised.
The University of Texas – Pan American
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Object-Oriented Programming: Classes and Objects
Tonga Institute of Higher Education
CIS16 Application Development and Programming using Visual Basic.net
Classes & Objects A deeper Look Chapter 10 & 11
Constructors & Destructors
Classes and Objects Systems Programming.
Object-Oriented Programming: Inheritance
Creating and Using Classes
Presentation transcript:

CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011

Objectives In this chapter, you will –Learn some examples of classes and objects Constructors Initializers Properties Shared class members 2

Recall: The Syntax for a Class Public Class Test '-----Variables '-----Methods '-----Properties '-----Events End Class 3

Classes Constructor Destructor Properties Methods 4

Constructor A constructor is a special member function whose task is to initialize the objects of its class A constructor call is required for every object that is created 5

Constructor (cont'd) Constructor must be named New Constructor are subroutines (Sub), as they cannot return values Default constructor –If you do not specify any constructor, the compiler provides one –The default constructor has no parameters –If you provide any constructors for a class, then the compiler will not provide a default constructor 6

Example of Constructor Public Sub New (ByVal initialBalance As Decimal) Public Sub New (Optional ByVal initialBalance As Decimal = 0D) 7

Example 9.2: Account.vb URL: – p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html Constructor –Exception Throw New ArgumentOutOfRangeException(“...”) 8

Property Values Get and Set Get allows you read the current property value Set allows you to change the property value Property without a "ReadOnly" or "WriteOnly" specifier must provide both a "Get" and a "Set" 9

Example 9.11: Time.vb URL: – 10/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_htp_20 10/codeexamples.html Properties –Public Property Hour() As Integer –Set accessor can check the value of input –"Hour" can be directly used Methods –Overloaded constructors / functions –E.g. ToString() 10

Overloaded Constructors Dim time1 As New Time() Dim time2 As New Time(2) Dim time3 As New Time(21, 34) Dim time4 As New Time(12, 25, 42) Dim time5 As New Time(time4) 'copy another Time object 11

Example 9.11: Time.vb (cont'd) When one object of a class has a reference to another object of the same class –The first object can access all of the second object’s data, method, and properties, including those that are Private –E.g., In the constructor New(ByVal t As Time): SetTime(t.hourValue, t.minuteValue, t.secondValue) hourValue, minuteValue, and secondValue are private member of class Time 12

With Statement With statement –Make multiple references to the same object in a concise manner –E.g., an object called time time.Hour, time.Minute, time.Second With time outputText1.Text=Convert.ToString(.Hour) outputText2.Text=Convert.ToString(.Minute) outputText3.Text=Convert.ToString(.Second) End With 13

Object Initializer Keyword: With Dim timeObject1 As New Time() With {.Minute = 33,.Second = 12} 14

Auto-Implemented Properties Public Property Hour As Integer –Visual Basic 2010 only (new feature) –The complier would generate a Private instance variable of type Integer named _Hour Public Property Hour As Integer Get Return _Hour End Get Set(ByVal value As Integer) _Hour = value End Set End Property 15

Using Me to Access the Current Object Me reference –The instance variable is sometimes "hidden" by some parameters or local variables with the same name –To refer instance variable, use Me Me.hour = hour Me.minute = minute 16

Garbage Collection Using … End Using statement –Using fileChooser As New SaveFileDialog() result=fileChooser.ShowDialog() fileName=fileChooser.Filename End Using 17

Example 9.13: Shared Member Class All objects of the class share the same value –E.g., counter in Employee object –Private Shared counter As Integer 'Employee objects Shared properties –No need to create an Employee object to call the Get method of a Shared property 18

Exercises Properties can contain both ______accessors. –1. Return and Value 2. Get and Value 3. Get and Set 4. Set and Return Keyword _____introduces a class definition. –1. NewClass 2. ClassDef 3. VBClass 4. Class The_____is used to retrieve the value of an instance variable –1. Get accessor of a property 2. retrieve method of a class 3. Client method of a class 4. Set accessor of a property A class can yield many _____, just as a primitive data type can yield many values –1. names 2. objects (instances) 3. values 4. types 19

Exercises (cont'd) Instance variables declared Private are not accessible_____. –1. outside the class 2. by other methods of the same class 3. inside the same class 4. All of the above. A class definition ends with the keyword(s)__. –1. Class End 2. End Class 3. EndClass 4. End Variables can be initialized ____. –1. when they are declared 2. to their default values 3. in a constructor 4. All of the above A(n) ____can ensure that a value is appropriate for a data member before the data member is assigned that value. –1. Get accessor 2. Access accessor 3. Modify accessor 4. Set accessor 20

21