Tonga Institute of Higher Education

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

Road Map Introduction to object oriented programming. Classes
1 Prototyping for HCI Spring 2004 (Week 8) Jorge A. Toro.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
5.05 Apply Looping Structures
Introduction to VB.NET Tonga Institute of Higher Education.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Working With Objects Tonga Institute of Higher Education.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Programming in Java CSCI-2220 Object Oriented Programming.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Working With Objects Tonga Institute of Higher Education.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Object-Oriented Programming: Classes and Objects.
Sub Procedures And Functions
Introduction to Object-oriented Programming
C# for C++ Programmers 1.
4. Java language basics: Function
Programming in visual basic .net Visual Basic Building Blocks
Classes (Part 1) Lecture 3
3 Introduction to Classes and Objects.
IS 350 Application Structure
Static data members Constructors and Destructors
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming: Classes and Objects
Java Programming: Guided Learning with Early Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Object-Oriented Programming: Classes and Objects
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Variables and Arithmetic Operations
VISUAL BASIC.
Chapter 6 Sub Procedures
Chapter 6 Multiform Projects
Chapter 3 Introduction to Classes, Objects Methods and Strings
CIS16 Application Development Programming with Visual Basic
Programming with Microsoft Visual Basic 2008 Fourth Edition
Tonga Institute of Higher Education
Tonga Institute of Higher Education
CHAPTER 6 GENERAL-PURPOSE METHODS
Tonga Institute of Higher Education
Classes and Objects.
Social Media And Global Computing Creating DLLs with Visual Studio
Workshop for Programming And Systems Management Teachers
Object-Oriented Programming: Classes and Objects
Tonga Institute of Higher Education
Methods.
How to organize and document your classes
Method of Classes Chapter 7, page 155 Lecture /4/6.
Object Oriented Programming in java
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Java Programming with BlueJ Objectives
STARTING OUT WITH Visual Basic 2008
Running a Java Program using Blue Jay.
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Web Programming and Design
ITM 352 Functions.
Creating and Using Classes
Presentation transcript:

Tonga Institute of Higher Education Creating Classes Tonga Institute of Higher Education

Current Status The form class contains all of our code Code is added to the form class Code may use variables, methods and events Code may use other objects When a program is run, the form object is used

Custom Classes and Objects Sometimes, the objects provided by Microsoft do not fit our needs. We can define custom objects. We can use custom objects just like normal objects

How to Define a Class Access Specifier - Covered later Class Name Class names should be nouns One word The first letter of each internal word is capitalized. Keep your class names simple and descriptive. Example: Customer SalesOrder

Demonstration Defining a class

How to Define a Variable Access Specifier Variable Name Type Access Specifier – Covered later Variable Name One word The first letter of each internal word is capitalized. Keep your variable names simple and descriptive. Example: FirstName PhoneNumber The type can be a primitive or an object Example: Integer, String, Boolean, Student

Demonstration Defining a variable

How to Define a Method (Subroutines and Functions) Methods - Pieces of code that perform a single function Subroutine – A method that does not return anything Function – A method that returns something

Difference Between Subroutines and Functions Access Specifier Parameter Type Method Name Parameter Name Access Specifier Method Name Parameter Type Parameter Name Return Type Return Data

Subroutines and Function Names Verbs One word The first letter of each internal word is capitalized. Keep your method names simple and descriptive. Example: RunFast GetBackground ShowWelcomeMessage

Subroutines and Function Parameters Parameters are optional Each parameter that your method accepts must have a name and type The name should be: One word The first letter of each internal word is capitalized. Keep your parameter names simple and descriptive. The type can be a primitive or an object Example: String, Integer, Customer Example: Private Sub ShowSummary(ByVal StudentID As Integer) If multiple parameters are used, separate them with commas Example: Private Sub ShowSummary(ByVal StudentID As Integer, ByVal StudentName as String) Use the name inside the method parameter list to access the parameter in your code StudentID StudentName

Function Return Types Access Specifier Method Name Parameter Type Parameter Name Return Type Return Data Return Type – Sets the type of data that will be returned from the function. The type of data can be a primitive or an object Example: String, Integer, Customer If you are not returning anything, do not use a function. Use a subroutine. Do not forget to use the Return keyword in your function

Demonstration Defining a subroutine

Demonstration Defining a function

Method Overloading Overloading - Having multiple methods with the same name but different parameters To make an overloaded method, create a method with the same name but different parameters. Return types for functions can be overloaded but it is not a good idea Same Name Different Parameters

Demonstration Method Overloading

Properties and Code Conventions Generally, make all class variables private Use properties to access your variables Properties – a specialized method used to get and set a class’ variables This could be done using regular methods, but properties are used more often

Regular Methods vs. Properties Same Thing!

Property Features Read-Only Properties Write-Only Properties

Demonstration Properties

Using the custom class Use custom objects just like normal objects Declare and instantiate them Use methods and properties A class cannot use itself A different class must use a class Example: A StudentSystem class would use a Student class

Demonstration Using the custom class

Dynamic Link Libraries Until now, all of our VB.Net programs have been .exe files This allows us to run a program Sometimes, we need to share a class that we’ve made A Dynamic Link Library is a file that contains classes for use by other programs. They end with .dll Example: System.dll

Seeing DLLs in Action When a form is run, many different classes are needed. Example: Form class, textbox class, and more. VS.Net loads dll files when a form is run. The dll files contain the class data for the objects needed to run the program The loading of dll files can be seen in the Output box

Demonstration Seeing DLLs in Action

Creating a DLL File We can create Dynamic Link Libraries Right click on the project in the Solution Explorer Select Properties Select the Class Library Output Type Click OK button Compile the project. When the project is compiled, VS.Net creates a .dll file in the Bin directory

Demonstration Creating a DLL File

References Many projects use dll files Many dll files used are listed in the references folder

Demonstration References

Adding DLL Files to a Project - 1 To use a dll file in a project, right click on the References folder and select Add Reference The Add Reference form is displayed

Adding DLL Files to a Project - 2 Use the .Net tab to add a .Net dll Use the COM tab to add a COM dll. (These are dll files made before VB.Net) Use the Projects tab to add a reference from a project in the same solution

Adding DLL Files to a Project Demonstration Adding DLL Files to a Project

Using DLL Files To use a class defined in a DLL file Add a reference to the dll file to the project Write the fully qualified namespace to access the object Dim x as new ClassLibrary1.Student Remember: Namespaces are not always the same as the file name! Class Name Fully Qualified Namespace

Demonstration Using DLL Files

Imports ClassLibrary1.Student Import Statements Import Statements allow you to not always type the fully qualified namespace Imports ClassLibrary1.Student There are import statements defined at the project level. To see these, go to Project Properties -> Common Properties -> Imports

Demonstration Import Statements

.DLL and .EXE File Tips A .Dll file cannot be run. Rather, it is built Do not use the same name for a .dll and exe file If the location of a .dll file reference is changed, VS .Net will show a yellow error message. To fix this, remove the incorrect reference and add a new reference When you make a change in a .dll file, the project must be rebuilt. When a .dll file is changed in a separate solution from the .exe file, the .exe solution must be reloaded An .exe project that uses a .dll file will have a copy of the .dll file saved to the bin directory. When giving the program to other people, both the .dll and .exe files must be given to run properly.

Demonstration .DLL and .EXE File Tips

What is Scope? The scope of an element (variable, class, method, etc.) defines what parts of a program can see it Using Public instead of Private changes the scope of this variable.

Levels of Scope Block Procedure Class And others

Block Scope A block is a set of statements terminated by an End, Else, Loop, or Next statement Example: The code within a For...Next or If...Then...Else...End. An element declared within a block can be used only inside that block Use Dim to declare elements with block scope The Static keyword may also be used (Covered later) Block

Demonstration Block Scope

Procedure Scope A procedure is a subroutine or function An element declared within a procedure is only usable in the procedure. Elements at this level are also known as local elements. Use Dim to declare elements with procedure scope The Static keyword may also be used (Covered later) Works like block scope but you must also consider the parameters Procedure

Demonstration Procedure Scope

Class Scope Declare elements at this level by placing the declaration statement outside of any procedure or block within the class. These access specifiers can be used Public Private Dim Friend Protected Protected Friend

Access Specifiers Public Private Dim Friend Protected Protected Friend Can be used by everything Private Can only be used by code inside the same class Dim Same as Private Friend Can be used by code inside the same project Access Specifier Name Dim FirstName as String Type Protected Can be used by code that inherits from this class Protected Friend Combination of Protected and Friend

Demonstration Access Specifiers

Scope: Global Variables vs. Local Variables Variables defined outside of a procedure or block but inside of a class Also called member variables Can be readonly by using the ReadOnly key word Local Variables Variables defined inside procedures or blocks They are only visible by code inside the procedure or block When looking for a value, start local. Go global if you can’t find the variable

Global vs. Local Variables Demonstration Global vs. Local Variables Have global and local variable with same names

Element Lifetimes The lifetime of a declared element is the period of time during which it is available for use. Local variables declared with Dim exist only while the procedure in which they are declared is executing. A variable declared in a class exists as a separate copy for each instance of the class in which it is declared; each such variable has the same lifetime as its instance.

Demonstration Element Lifetimes Have global and local variable with same names

Controlling Element Lifetimes We can additionally control the lifetime of a declared element Code Setting variables to nothing Keywords Shared Static

Setting variables to nothing Demonstration Setting variables to nothing Have global and local variable with same names

Shared Lifetime 1 Instance variable - A variable where each instance can have a different value

Shared Lifetime 2 Global Variables may be shared Shared elements are not associated with a specific instance of a class. All instances use the same value. Access them 2 ways Use the class name Use the variable name of a specific instance of the class

Demonstration Shared Lifetime

Static Lifetime Local variables may be static Static variables remain in existence and retain their latest values after termination of the procedure in which they are declared.

Demonstration Static Lifetime

Constructors 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) Show Java Doc

Default Constructors All classes have a default constructor The default constructor is a constructor that accepts no parameters Automatically called when the object is created Default constructors go away when you define your own constructors

Creating Constructors Constructors do not return anything. To overload constructors Define multiple methods with the same name as the class with different parameters New indicates that this is a constructor Overloaded

Creating Constructors Demonstration Creating Constructors