Академический Документы
Профессиональный Документы
Культура Документы
VB.NET PROGRAMMING
TOPIC 4
Object
Objects included in Visual Basic .NET make the development of complex applications easier because you do not have to concern yourself with the creation of objects and their properties. Visual Basic .NET allows you to create your own objects both by code and visually from existing objects contained within Visual Basic .NET.
Daniel
Order
Encapsulation
Encapsulation enables the programmer designing the class to dictate what types of operations are permissible upon a class without permitting other programmers access to the inner workings of the class. The class designer allows other programmers to have the complexity of a class hidden and ensures that other programmers can only use the class in ways intended by the programmer designing the class. You have used objects like text boxes, labels, and buttons without knowledge of their internal workings.
The properties and events accessed only allowed you to manipulate the objects in ways predetermined by the developers of the objects.
Polymorphism
The concepts of having one name but a different behavior depending upon the object it belongs to is known as polymorphism. Many of the controls that you have built your application from have properties, methods, or events with the same name but take on a slightly different meaning from control to control. You have been using polymorphism when you perform the basic mathematical operations. Regardless of the data type you wish to perform addition or subtraction on, you use operators like + or -. You do not require a different operator to perform an addition for each data type. When you design objects that have similar functions, it is helpful to design them with polymorphism in mind.
Inheritance
One of the keys to efficient development of applications is the ability to reuse the code that you previously developed.
One way programmers accomplish this is to cut and paste code from one project to another.
A better method, inheritance, is to build new classes from previous ones without modifying the original code. If you use the former method and then later discover a bug in the original code, you must go back and modify the code in all the classes you copied the bug into. With inheritance, if you modify the original class, all the classes based on that change would be updated automatically. The other problem, although not quite as severe, is the waste of a lot of unnecessary disk space by having multiple copies of the source code each time you copy it.
Visual Basic .NET comes with many built-in objects ready for you to use. Not all objects are visual in nature. There are many ways of adding objects to your application. The most intuitive way to add an object to your application is to drag a predefined object onto your form. This only works with objects built into the toolbox. There are other ways you can declare and instantiate objects.
You are already familiar with declaring variables of the type String Dim strName As String
When you declare an object in this way, it is considered a structure or value type variable. There is only a subtle difference between using value and reference type objects. You only need to concern yourself with the difference in declaring, allocating, and reallocating value type objects from reference type objects. Value type variables are automatically reallocated when the routine they are declared within is exited.
There are a great many routines that can be executed upon the data stored in the String built into the String class. These routines are commonly referred to as methods of the class. In general, methods come in two varieties. Some methods and functions perform a series of tasks and pass a value back to the calling routine. Other methods and subroutines will perform a series of tasks but not pass a value back. To access a lit of methods of a class, just type a period after the objects name.
Return Value
INPUT STRING ALL LOWERCASE ALL UPPERCASE UPPER AND LOWERCASE
strStringVariable = Hello, my name is Inigo Montoya. You killed my father. Prepare to die. strStringVariable.Length()
strStringVariable = strStringVariable.Length()
70
Example:
Invoke Method Return Value
is
the
Stri
the String
A message box can be used to display a message and to get a response MessageBox class
Member of System.Windows.Forms namespace Has a single method named Show Creates an instance of MessageBox class Makes a message box appear
Show method
First argument Message to be displayed Second argument Caption to be displayed Third argument Specifies the buttons to be displayed Fourth argument Specifies the type of icon displayed
Indicates which button was clicked by user Is of data type DialogResult Can be compared to specific values using an If statement
Classes in Visual Basic .NET can be created in a few ways. One way is to crate a class in code. A class can be thought of as being composed of three sets of definitions. To define a class and all of its attributes and methods, you enclose all of the contents using the following syntax: Public Class ClassName 'Properties definitions go here 'Property Get/Set statements go here 'Methods go here End Class
Step 3: You must specify the properties of the class. Properties are in essence the variables of the class. For the Counter class, only one value must be stored, the current state of the counter. Therefore, only one property is required. An Integer property must be created to store the current value of the counter. The property is declared within the class definition. A property is defined with the following syntax:
The Scope of a property determines how visible this property is to other parts of the application. The scope can be Public, Private, or Protected. A Public scope gives the entire application access to the property, while a Private scope only allows methods from the class the property is defined within to have access to it. The Protected scope deals with more advanced concepts in object-oriented development and will be skipped for now. The rules for specifying a data type for a property are the same as for declaring a variable. Therefore, a data type can be an Integer, Single, String, text box, and so on. Declare all of the properties for your Counter class:
Public Property PropertyName() As Datatype Get Return PropertyVariableName End Get Set PropertyVariableName = Value End Set End Property The PropertyName is the name of the property as it appears to the developer. The PropertyVariableName is the name of the property variable accessible only within the class and its methods. If there are no rules restricting the values that can be set to a property, then your Set statement can simply assign the quantity being passed in the Value parameter to the Private property.
Get and Set Continued The Get and Set routines for the Counter class are as follows: Public Property CurrentValue() As String Get Return mintCurrentCount End Get Set(ByVal Value As String) If (Value = 0) Then mintCurrentCount = Value Else MsgBox("You have attempted to set the counter to a value " &_ " other than 0") End If End Set End Property
Writing method
You need the ability to perform operations upon the properties stored within the class. A method is an easy way to accomplish this. By providing public methods to a class, you can control the way developers access the properties you specify. You have two choices as to the type of method that you can create. The simplest is a subroutine. The syntax for writing a simple subroutine method is as follows:
Example: You want to create a method that allows the user to call a Reset routine that sets the counter back to 0: 'Method to reset the counter to 0 Public Sub Reset() mintCurrentCount = 0 End Sub Here is the code to increment or decrement the counter:
'Method to add 1 to the counter Public Sub Increment() mintCurrentCount += 1 End Sub 'Method to subtract 1 from the counter Public Sub Decrement() mintCurrentCount -= 1 End Sub
Once an object has been instantiated, you will want to set its individual properties. With an object declared from code you do not have the option of using the Properties window. You need another syntax:
objName.PropertyName = PropertyValue
ThursdayHours
FridayHours HourlyWage
7.5
8 125.00
Dim JeffSalvage As New Employee() JeffSalvage.FirstName = "Jeff" JeffSalvage.LastName = "Salvage" JeffSalvage.MondayHours = 7 JeffSalvage.TuesdayHours = 9 JeffSalvage.WednesdayHours = 8.5 JeffSalvage.ThursdayHours = 7.5 JeffSalvage.FridayHours = 8 JeffSalvage.HourlyRate = 125
Calling a method
Once an object has been instantiated, its methods can be called. Calling a method is often referred to as invoking a method and can be accomplished using the following syntax: objName.MethodName() Example: To invoke the method OutputWeeklyPay of the object JeffSalvage, use the following code: JeffSalvage.OutputWeeklyPay() To invoke the method OutputWeeklyPay of the object JeffSalvage with the parameter Monday, use the following code: JeffSalvage.OutputWeeklyPay(Monday)
Overriding method
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.
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
Exercise
1) Match the following items to the closest example. A inheritance B polymorphism C encapsulation D instantiation E class 1 When a function takes on different meanings based upon what object is being used. 2 The hiding of the details of the implementation from a developer. 3 The ability to create a new class based on the definition of an existing one. 4 The blueprint of an object. 5 The creation of an object from a class.
Exercise
2) Which of the following calls to the methods of the String class are correct? A strStringVariable.ToUpper() B strStringVariable.ToUpper(This will work) C strStringVariable.Length(10) D strStringVariable.Trim() E strStringVariable.SubString(This will work, 10) F strStringVariable.SubString(This will work, 10, 2) G strStringVariable.SubString(10) H strStringVariable.SubString(10, 2) I strStringVariable.GiveMeAnA()
Exercise
3) What is the output of the following code? a.
Exercise
4) Given the following definitions of three methods, show which of the following method calls are valid:
Private Sub DrillMethod1(ByVal intDrillValue As Integer, _ ByVal strDrillValue As String) End Sub Private Sub DrillMethod2(ByVal strDrillValue As String, _ ByVal intDrillValue As Integer) End Sub Private Sub DrillMethod3(ByVal strStringValue As Integer, _ ByVal intIntegerValue As String) End Sub
a) b) c) d) e) f)
DrillMethod2(intVal1, intVal3)
DrillMethod2(sngVal4, intVal1) DrillMethod2(strVal2, intVal1) DrillMethod3(intVal1, strVal2)