Pemrograman VisualMinggu …3… Page 1 MINGGU Ke Tiga Pemrograman Visual Pokok Bahasan: Class,...
-
Upload
karly-nemo -
Category
Documents
-
view
221 -
download
1
Transcript of Pemrograman VisualMinggu …3… Page 1 MINGGU Ke Tiga Pemrograman Visual Pokok Bahasan: Class,...
Minggu …3… Page 1
Pemrograman Visual
MINGGU Ke Tiga
Pemrograman VisualPemrograman Visual
Pokok Bahasan:Class, Objects, Methods and Instance Variable
Tujuan Instruksional Khusus:Mahasiswa dapat menjelaskan dan mengaplikasikan konsep class dan object pada Visual Basic 2008
Referensi:Deitel Deitel, Visual Basic 2008 (2009), How to Program, Prentice Hall. Chapter 4
Pemrograman Visual Minggu …3… Page 2
Agenda• Declare class with a method and
instantiating an object of class• Declare a method with parameter• Instance variable and properties• Value Types and Reference Types• Initializing Objects with constructors• Validating data with set accessors in
properties
Pemrograman Visual Minggu …3… Page 3
Declare class with a method and instantiating an object of class
• Adding a class to a Visual Basic Project– Right click the project name in the solution
explorer and select add > class from the menu that appears
• Example of Class GradeBook
Pemrograman Visual Minggu …3… Page 4
Declare class with a method and instantiating an object of class (Continued …)
• Using Class GradeBook
– You’ll now use class GradeBook in an apllication. A Visual Basic project that contains only class GradeBook is not an application that can be exceuted, because GradeBook does not contain main. If you try to compile such a project, you’ll get an error message like:
‘Sub Main’ was not found in ‘GradeBook.GradeBookTest’
• So How we can fixed the Problem???
– We must declare a separate class or module that contains main, or we must place main in class GradeBook
Pemrograman Visual Minggu …3… Page 6
Declare a Method With Parameter
• What is a Parameter ???
• Example parameter of Class GradeBook
Pemrograman Visual Minggu …3… Page 8
Instance Variables and Properties• In Previous discussion, we declare all of an
application’s variable in the main method. Variables declared in the body of a particular method are known as local variables and can be used only in that method. When a method terminates, the values of its local variables are lost.
• So How we can fixed the Problem???– We must create attributes exist before a method is called on
the object, while the method is executing and after the method completes execution.
Pemrograman Visual Minggu …3… Page 10
Example execution Class GradeBook using instance variables and properties
Pemrograman Visual Minggu …3… Page 11
Value Type and Reference Type• Data type in Visual Basic are divided into two categories –
value types and reference type
Dim count as Integer = 7
A variable (count) of a value type (Integer)
Contains a value (7) of that type
Pemrograman Visual Minggu …3… Page 12
Value Type and Reference Type (Continued …)
• A variable of a reference type (sometimes called a reference) contains the address of a location in memory where an object is stored
• Reference type instance variables are initialized by default to the value nothing
• A client of an object must use a reference to the object to invoke (i.e.,call) the object’s methods and access the object’s properties
Is a object
Pemrograman Visual Minggu …3… Page 13
Initializing Objects with Constructors
• Each class you declare can provide a constructor that can be used to initialize an object of the class when the object is created
• Keyword new calls the class’s constructor to perform the initialization
• By default, the compiler provides a default constructor with no parameter in any class that does not explicitly include a constructor
• When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class
Pemrograman Visual Minggu …3… Page 14
Initializing Objects with Constructors (Continued …)
• A constructor must have the name New• Constructor declarations cannot return values• A class’s instance variables are initialized to their
default values if no constructor is provided
Pemrograman Visual Minggu …3… Page 17
Validating Data with Set Accessors in Properties
• Properties can be used to keep an object in a consistent state, that is a state in which the object’s instance variables contains valid data
• A class’s set accessors cannot return values indicating a failed attempt to assign invalid data to objects of the class
• In Visual basic, it is possible to declare the get and set accessors of a property with different access modifiers. In this case, one of the accessors must have the same access as the property and the other must be more restrictive than the property. Private is more restrictive than public.