Contenu connexe
Similaire à Vb.net session 03
Similaire à Vb.net session 03 (20)
Vb.net session 03
- 1. Implementing Object Oriented Programming
In Visual Basic.NET
Pre-Assessment Questions
• Consider the following statements:
• Statement A: There are two types of user interfaces, character user
interface and (CUI) and graphical user interface (GUI).
• Statement B: In CUI, you can interact with an application by entering
commands.
Which of the following is correct with respect to the above statements?
d. Both, Statement A and Statement B, are False.
e. Both, Statement A and Statement B, are True.
f. Statement A is True and Statement B is False.
g. Statement A is False and Statement B is True.
©NIIT Introduction to VB.NET Lesson 2A / Slide 1 of 27
- 2. Implementing Object Oriented Programming
In Visual Basic.NET
Pre-Assessment Questions (Contd.)
2. Which of the statements is false?
• A Windows Form is a representation of any window displayed in an
application.
• Windows form properties are used to determine its appearance at
compile time.
• Every Window Form is a class derived from Form class in
System.Windows.Forms namespace.
• The form class is derived from the control class.
©NIIT Introduction to VB.NET Lesson 2A / Slide 2 of 27
- 3. Implementing Object Oriented Programming
In Visual Basic.NET
Pre-Assessment Questions (Contd.)
3. Which of the following data types can contain values between 0 and 65535?
• Boolean
• String
• Char
• Decimal
• In the following code
Dim Result as Boolean
Result = X<Y
• The Result will have the true value
• The Result will have false value
• The Result will have either true or false value
• The Result will have no value
©NIIT Introduction to VB.NET Lesson 2A / Slide 3 of 27
- 4. Implementing Object Oriented Programming
In Visual Basic.NET
Pre-Assessment Questions (Contd.)
5. Data types like Boolean,String or Integer are
• Composite data types
• System data types
• Object data types
• Array data types
©NIIT Introduction to VB.NET Lesson 2A / Slide 4 of 27
- 5. Implementing Object Oriented Programming
In Visual Basic.NET
Solutions to Pre-Assessment
Questions
• b.
• b.
• c. Char
• a.
• b. System data type
©NIIT Introduction to VB.NET Lesson 2A / Slide 5 of 27
- 6. Implementing Object Oriented Programming
In Visual Basic.NET
Objectives
In this lesson, you will learn to:
• Identify classes and objects
• List the advantages of using classes and objects
• Declare and import namespaces
• Implement abstract classes in Visual Basic .NET
• Declare structures and interfaces
• Create and instantiate a class
• Create an inherited form in Visual Basic .NET
• Implement inheritance in VB .NET
©NIIT Introduction to VB.NET Lesson 2A / Slide 6 of 27
- 7. Implementing Object Oriented Programming
In Visual Basic.NET
Understanding Object-Orientation
Concepts
• Visual Basic.Net is an object-oriented programming language.
• Visual Basic .NET supports all the four features of object-oriented
programming.
• The features of object-oriented programming are
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism.
©NIIT Introduction to VB.NET Lesson 2A / Slide 7 of 27
- 8. Implementing Object Oriented Programming
In Visual Basic.NET
Understanding Classes
• A class is a conceptual representation of all the entities that share
common attributes and behaviors.
©NIIT Introduction to VB.NET Lesson 2A / Slide 8 of 27
- 9. Implementing Object Oriented Programming
In Visual Basic.NET
Object
• An object is an instance of a class
• All the objects of a class have individual copies of the attributes and share a
common set of behaviors
©NIIT Introduction to VB.NET Lesson 2A / Slide 9 of 27
- 10. Implementing Object Oriented Programming
In Visual Basic.NET
Advantages of Using Classes and
Objects
• Maintenance of code by introducing modularity
• Encapsulation of internal complexities in code from end-users
• Reusabilty
• Support for a single interface to implement multiple methods
©NIIT Introduction to VB.NET Lesson 2A / Slide 10 of 27
- 11. Implementing Object Oriented Programming
In Visual Basic.NET
Constructors
• Constructors are special methods that allow control over the initialization of
objects.
• A shared constructor will not run more than once during a single execution of a
program.
• When an instance of a class is created, the run-time environment executes the
instance constructors.
©NIIT Introduction to VB.NET Lesson 2A / Slide 11 of 27
- 12. Implementing Object Oriented Programming
In Visual Basic.NET
Destructors
• Destructors are special methods that are used to release the instance of a
class from memory.
• There are two types of destructors in Visual Basic .NET, Finalize() and
Dispose().
• The sequences in which the constructors and destructors are invoked are:
• Shared constructor of the inherited class
• Shared constructor of the base class
• Instance constructor of the base class
• Instance constructor of the inherited class
©NIIT Introduction to VB.NET Lesson 2A / Slide 12 of 27
- 13. Implementing Object Oriented Programming
In Visual Basic.NET
Namespaces
• Namespaces enable you to avoid name collisions.
• Every project in Visual Basic .NET has a root namespace, which is set in the
Property page of the project Using Namespaces.
• You can also organize classes using the Namespace keyword as shown below.
Namespace CustNameSpace
Class CustClass
End Class
End Namespace
• You can use namespaces explicitly through direct addressing or implicitly
through the Imports statement.
©NIIT Introduction to VB.NET Lesson 2A / Slide 13 of 27
- 14. Implementing Object Oriented Programming
In Visual Basic.NET
Abstract Classes in Visual Basic .NET
• Visual Basic .NET enables you to create abstract classes that contain the
skeleton of the methods implemented by the derived class.
©NIIT Introduction to VB.NET Lesson 2A / Slide 14 of 27
- 15. Implementing Object Oriented Programming
In Visual Basic.NET
Understanding Structures
• A structure is used to create user-defined data types.
• You can declare a structure when you want a single variable to hold multiple
types of related data.
• Data can be stored in and retrieved from a structure.
©NIIT Introduction to VB.NET Lesson 2A / Slide 15 of 27
- 16. Implementing Object Oriented Programming
In Visual Basic.NET
Interfaces in Visual Basic.NET
• Interfaces are inheritable in Visual Basic.Net.
• An interface defines properties, methods, and events.
• You declare an interface using the Interface and End Interface statements.
• You can declare only methods, functions, properties, and events in an
interface.
©NIIT Introduction to VB.NET Lesson 2A / Slide 16 of 27
- 17. Implementing Object Oriented Programming
In Visual Basic.NET
Interfaces in Visual Basic.NET(Contd.)
• An interface can inherit members from an existing interface
• The members of an interface consist of the declared members and the
members inherited from its base interfaces.
©NIIT Introduction to VB.NET Lesson 2A / Slide 17 of 27
- 18. Implementing Object Oriented Programming
In Visual Basic.NET
Inheritance
• The inheritance feature allows you to define a new class by extending an
existing class.
©NIIT Introduction to VB.NET Lesson 2A / Slide 18 of 27
- 19. Implementing Object Oriented Programming
In Visual Basic.NET
Polymorphism
• The concept of using operators or functions in different ways depending on
what they are operating on is called polymorphism.
©NIIT Introduction to VB.NET Lesson 2A / Slide 19 of 27
- 20. Implementing Object Oriented Programming
In Visual Basic.NET
Demo for
Creating a Class in Visual Basic.Net
©NIIT Introduction to VB.NET Lesson 2A / Slide 20 of 27
- 21. Implementing Object Oriented Programming
In Visual Basic.NET
Problem Statement
• A company called Protec Inc. needs to maintain customer information. The
details of the customer need to be accepted through a graphical interface. The
user interface can be either Windows Forms, Web Forms, or Console. The
customer information also needs to be stored in relevant memory variables of
a class. The information should also be retrieved and displayed to the user.
The details of the customer will include CustomerID, First Name, Last
Name, Address, Telephone number and E-mail Id.
©NIIT Introduction to VB.NET Lesson 2A / Slide 21 of 27
- 22. Implementing Object Oriented Programming
In Visual Basic.NET
Solution
• A user interface screen is used to accept data from the user and displaying
data to the user. A class can be used to store and retrieve data from the
database. Perform the following steps to create a class:
• Create a user interface.
• Adding a class to the project
• Write the code to store and retrieve data from the class
• Save and run the application
©NIIT Introduction to VB.NET Lesson 2A / Slide 22 of 27
- 23. Implementing Object Oriented Programming
In Visual Basic.NET
Demo for
Implementing Inheritance
©NIIT Introduction to VB.NET Lesson 2A / Slide 23 of 27
- 24. Implementing Object Oriented Programming
In Visual Basic.NET
Problem Statement
• The company Protec Inc needs data entry forms to store information in the
Customers, Orders, and Query Handling databases. The data entry forms
should have a similar user interface with the Reset and Exit buttons.
Incorporate the interface for the Order details form.
©NIIT Introduction to VB.NET Lesson 2A / Slide 24 of 27
- 25. Implementing Object Oriented Programming
In Visual Basic.NET
Solution
• To create the user interface form you need to perform the following steps:
• Create the user interface screen
• Add code for the controls
• Create an inherited form based on the base form
• Add an inherited form to the project
• Add the additional user interface control
• Display a similar user interface
• Add code for the inherited controls
• Save and Execute the application
©NIIT Introduction to VB.NET Lesson 2A / Slide 25 of 27
- 26. Implementing Object Oriented Programming
In Visual Basic.NET
Summary
In this lesson, you learned that:
• Visual Basic .NET is an object-oriented programming language
• Classes can be added to a Visual Basic .NET project
• An object is an instance of a class
• Advantages of Using Classes and Objects
• Maintenance of code
• Encapsulation
• Reusabilty
• Support for a single interface to implement multiple methods
• Constructors are special methods that allow control over the initialization of
objects.
• Destructors are special methods that are used to release the instance of a
class from memory.
©NIIT Introduction to VB.NET Lesson 2A / Slide 26 of 27
- 27. Implementing Object Oriented Programming
In Visual Basic.NET
Summary (Contd.)
• An important advantage of using a namespace is the prevention of a name
collision
• Abstract classes are used to define the skeleton of the methods that the
derived class can implement.
• A structure is used to create user-defined data types.
• An interface enables you to separate the definition of objects from their
implementation so that the objects can evolve without the risk of introducing
incompatibility in existing applications.
• The inheritance feature allows you to define a new class by extending an
existing class
• The concept of using operators or functions in different ways depending on
what they are operating on is called polymorphism.
• Interfaces and classes are inheritable in Visual Basic .NET
©NIIT Introduction to VB.NET Lesson 2A / Slide 27 of 27