150 likes | 300 Views
OOP In Depth Subroutines and Functions Overview Of Classes Creating a Class Creating a Method Overloading Methods Override a Method Constructors Overloading Constructors Inheritance Creating an Inherited Class . Subroutines and Functions. Private Sub ShowDetails(ByVal strLoc As String)
E N D
OOP In DepthSubroutines and FunctionsOverview Of ClassesCreating a ClassCreating a MethodOverloading MethodsOverride a Method Constructors Overloading ConstructorsInheritance Creating an Inherited Class Prepared By Hafeez Mohammed
Subroutines and Functions • Private Sub ShowDetails(ByVal strLoc As String) • lblLoc.Text = strLoc • lblTiming.Text = "8AM to 10PM" • End Sub • Public Function Total() As Integer • ' No Arguments passed • Total = ((mintAdults * mintAdultRates) + (mintKids * mintKidRates)) * mintNumDays • End Function Prepared By Hafeez Mohammed
Overview Of Classes • Many Classes per file • Can be inside forms, modules, class • Every Class has a Constructor called New • It replaces VB6.0 Class_Initialize routine • Passing data to the constructor • Constructors can be overloaded • Destructor is called Finalize • It cannot be called explicitly • It is only called by GARBAGE COLLECTOR which can be used to release the resources Prepared By Hafeez Mohammed
Overloading Methods • Overloading a method is creating the same method many times each time with different parameters • These methods can be called uniquely depending upon the parameters we pass • The Domestic Form in our ITMResorts project, we make use of the “Total” method and we overloaded it as many times as we intended. Prepared By Hafeez Mohammed
Overloaded Function • Public Function Total() As Integer ' No Arguments passed • Total = ((mintAdults * mintAdultRates) + (mintKids * mintKidRates)) * mintNumDays • End Function • Public Function Total(ByVal mintPercent As Integer) As Integer ' Overloaded with one argument • Total = ((mintAdults * mintAdultRates) + (mintKids * mintKidRates)) * mintNumDays • Total = Total - ((Total) * mintPercent / 100) • End Function • Public Function Total(ByVal mintPercent As Integer, ByVal mintSeasonalDiscount As Integer) As Integer • 'Overloaded with two argument • Total = ((mintAdults * mintAdultRates) + (mintKids * mintKidRates)) * mintNumDays • Total = Total - ((Total) * mintPercent / 100) • Total = Total - ((Total) * mintSeasonalDiscount / 100) • End Function Prepared By Hafeez Mohammed
Overloaded Constructor • From our Project Form we have seen how Sub NEW() constructor of a class can be overloaded • Overloading is basically achieved by passing different sets of parameters under different scenarios Prepared By Hafeez Mohammed
Thank God! Inheritance Finally • A Class supporting Inheritance is said to be having all the properties and methods from the other class • This new class not only will have the properties and methods of the BASE class but can have its own properties and methods as well • Inheritance with Overriding is the ability of a class to override the methods of the BASE class • It can also replace the functionality of the BASE class • Inheritance can be classified in to the following types - Implementation Inheritance - Visual Inheritance Prepared By Hafeez Mohammed
Inheritance in depth • Inheritance helps you prevent duplicating code, Ex:Common properties like FirstName, LastName, Addresses can find application in many forms, we can create one class with the above properties and “Inherit” it into many other classes, so that we need not have many classes defined each for Students, Instructors, Employees forms • Another important application is easy maintainability, for instance if you need to make a change do it in your “BASE” class and all inherited classes will reflect that change • New Class is also called “Child Class”,”Sub-Class” or a “Derived Class” • Original Class is called “Parent Class”, Super-Class” or a ”Base Class” Prepared By Hafeez Mohammed
Lets Build an Inherited Class • From our previous class “RateEvaluater”, lets create an inherited class called “CANRateEvaluater” • Notice that we can use the new class and pretty much get the same functionality • Lets Add more functionality to this new “CANRateEvaluater” Class • Lets Add a new property called “ExchangeRate” (US to CAN Dollar Exchange Rate for a particular day) to this new class • Also Add another property called “ServiceCharge” • Add a method “CANTotal” which will internally call “Total” from the “Base Class” and then divide it by ExchangeRate and finally add the ServiceCharge to get the CanadianTotal Prepared By Hafeez Mohammed
Overriding a function • In VB.NET we can override a “BASE” Class methods by creating the same name methods in the “DERIVED” Class and using the Overrides and Overloads keywords • Lets look into our “ShowHeader” method from our Project and see how this can be overloaded Prepared By Hafeez Mohammed
Error Handling in VB.NET • The .NET Framework provides “structured exception handling” using the TRY, CATCH,FINALLY and THROW keywords in VB.NET • Compare to VB6.0’s On Error GOTO and Resume Next this new exception handling is by far superior • Exception handling in .NET is based on the “EXCEPTION” Class, which contains information of not only the current error but a linked list of errors that may have triggered the current error • Every Class in .NET framework throws exceptions when it encounters run-time errors and using the exception class we can very easily handle all such errors • Everything revolves around TRY, CATCH and FINALLY blocks Prepared By Hafeez Mohammed
Error Handling in depth • If our code does not include any error handling, any errors that occur at run-time bubble back to the .NET runtime which will show a very confusing dialog box, we can overcome this by adding exception handling using a simple “TRY/CATCH” block • With the TRY/CATCH block, if an error occurs within the TRY block, execution will go to the CATCH block where we can use the show method of the MessageBox class to gracefully handle the error and display the text • To exactly determine what error occurred we can use the EXCEPTION Class which provides the info about the error • Some of the members of the Exception Class are “Message”, “ToString”, “InnerException” and “StackTrace” Prepared By Hafeez Mohammed
Creating Class Libraries • Class Library is basically creating a DLL with one or more classes in it, this DLL is also called a component • This DLL can be consumed by an EXE or another DLL • Components run in the same process space as the caller • Can be used by many processes (at the same time) Ex: Many EXE’s can use this DLL which can have many classes in it - Reusable - Maintainable - Redistribution is much easier • Components can be built to perform “DATA ACCESS” - Many Classes on for each Table - Each such class can have “Add”, “Delete”, …. functionality built inside it Prepared By Hafeez Mohammed
Namespace • Namespaces organizes the object references across the .NET infrastructure in an hierarchical manner • Remember the way we used “ProgID” in VB6.0 to access a particular object, Namespace in .NET comes with a similar approach and with the same concept • Looking at the Hierarchy, We will have the DLL/Component and inside it a namespace and inside the namespace we will have one or more classes • It’s an extra layer which helps in grouping classes together or organizing them, in other words is a logical extension of the COM naming structure • In a DLL we can have one or more than one namespace/s • DLLName.Namespace.Classname • Ex: Microsoft.VisualBasic.Compatibility.VB6 Prepared By Hafeez Mohammed
Lets Build a .NET Component and Use it • Lets build a component in VB.NET by opening a new project as the class template, call the component MoveMoney • Public Class MoveMoney basically will be Transferring the funds from the IndianAccount to the USAccount using this component • Our MoveMoney component has a method “DepositMoney” • Public Function DepositMoney(ByVal intDollars As Integer) As Boolean • DepositMoney basically has two private functions called - Private Function TakeFromIndianAccount(ByVal intDollars As Integer) As Boolean -Private Function AddToUSAccount(ByVal intDollars As Integer) As Boolean Prepared By Hafeez Mohammed