Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.

Similar presentations


Presentation on theme: "Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data."— Presentation transcript:

1 Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data

2 Slide 2 Objectives Describe these members of a class: constructor, method, field, and property Explain how instantiation works Describe the concept of overloading a method Explain what a shared member is

3 Slide 3 Common Class Elements (repeated) Classes contain other elements: Procedures (methods) – functions and subs – operations that can be performed by an object. Constructors – special type of method that’s executed when an object is instantiated Data stores (including properties) Other forms – objects of the form data type Numerous objects of types defined by other classes Variables (of primitive types) Events (and Event handlers -- not always tied to forms) – an event is a signal that notifies other objects that something noteworthy has occurred

4 Slide 4 Common Class Elements (continued - repeated) User defined data types – structures, enumerations, complex structured types (arrays of structures, lists or arrays of objects, etc) Delegate - A special type of object that’s used to wire an event to a method Data Stores Property - Represents a data value associated with an object instance Field - A variable that’s declared at the class level Constant- A constant associated with the class Default property - A special type of property that is used by default if a property name isn’t specified. It must include a parameter, which is typically used to access individual items within a class that represents a collection of objects. Operator- A special type of method that’s performed for a Visual Basic operator such as + or =.

5 Slide 5 How Instantiation Works The process of creating an object from a class is called instantiation You can create many instances of a single class A class defines a reference type The variable that is created for the object of a class thus contains a reference to the memory location where the object is stored and not the object itself When an object is instantiated the appropriate constructor is executed

6 Slide 6 Fields Fields are class variables that are global to (accessible by) all of the executable components of a class Usually declared as private and are not accessible outside the class May also be declared as public or protected and may also be declared using the friend modifier

7 Slide 7 Fields (continued) The desirable goal in using classes is to encapsulate and protect class data stores from external reference except through methods of the class Declaring fields as public defeats the purpose of using classes If accessing some class data stores from outside the class is considered cumbersome, we can access them through the use of properties, which simplifies the access mechanism we have to use while still protecting the data

8 Slide 8

9 Slide 9

10 Slide 10 Shared versus Instance Data Class data can be instance data or shared data Shared data is declared with the Shared keyword One copy exists no matter the number of class instances One copy of instance data exists for each class instance How are shared data useful? 

11 Slide 11 A Note on Designing with Shared Data NOTE: A shared function operates within the context of shared data – it can access any shared data in a class, but it cannot access instance members How is the shared counter NextHuman used below?

12 Slide 12

13 Slide 13 Using Properties to Access Private Data If we wish to provide more convenient access to the private or protected data of a class, we can do so through the use of public properties

14 Slide 14

15 Slide 15 Different Types of Properties

16 Slide 16 Accessing Properties

17 Slide 17 Methods Classes contain subs and functions The public subs and functions are called methods There also can be private subs and functions I call these methods as well (although the book does not seem to) Why might we want a private method? Methods perform operations on the data stores (attributes) of a class

18 Slide 18 Method Signatures and Overloading A method’s signature is defined by its name and its unique combination of parameters Note that the return value is not involved – it is assumed to always be the same We can write methods having the same name but different sets of parameters  This is known as overloading Overloading provides multiple ways of invoking a given method

19 Slide 19 Methods and Overloading

20 Slide 20 Overloading and Intellisense How does Intellisense handle overloaded functions (when it shows you the parameter list)

21 Slide 21 Constructors By default, VB will provide you with a default constructor for each class you write When you use new to create a new object (an instance of a class), the VB default constructor assigns default values to all instance variables of the new object If this is not what you want, you can write your own constructor(s) by taking advantage of overloading How does the compiler know which constructor is being referenced when you use New applied to a type (class) with multiple constructors

22 Slide 22 Overloading Constructors

23 Slide 23 More on Constructors

24 Slide 24 Murach’s Visual Basic 2005, C11 © 2006, Mike Murach & Associates, Inc. Slide 24

25 Slide 25

26 Slide 26

27 Slide 27 Class Shared Members (optional)

28 Slide 28

29 Slide 29

30 Slide 30

31 Slide 31


Download ppt "Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data."

Similar presentations


Ads by Google