Object oriented programming features in vb.net

Object oriented programming features:

1) Data Abstraction or Data hiding:

It is used to provide the relevant information and used to hide the relevant information from the user.

To achieve: Class or structures should be used

 

2) Polymorphism:

Whenever an entry can change its definition based on the arguments values supplied for that entity then it is said to be supporting polymorphism.

To achieve: Overloading concept is used to achieve polymorphism.

 

Note:

If any language supports the above 3 features then it is said to be an object based programming language.

 

Ex: VB 6.0

 

Inheritance:

It is used to reuse or to redefine the existing class definition.

 

Note:

If any language supports the above all features then it is said to an object oriented programming language.

 

Ex: VB.Net

 

Properties:

It is used to set or get the values from the private or protected members of the class

 

Syntax:

Public [Read only Write only] property propertyName () as data type

Set (by val value as data type) [property should be always be public]

Variable = value

End set

Get

Return variable

End get

End property

Value in the above syntax is a keyword which maintains the value assigned by the user at the runtime.

  • In order to define Read only property then “get” block should be used with “Read only” keyword.
  • In order to define Write only property then “set” block should be used with “Write only” keyword.

Product [class]

= product Id

= product Name

= price

Sub new (Pid, Pname, Price) [we can’t change pid, pname, price can be fixed or re written]

Read Only

Product Id

Product Name

Read & write only

Price

 

 

Open new app

‘‘Demo on properties

Imports system

Module property ‘‘Demo

Class Product

Private - Product Id As integer

Private – Product Name As string

Private – Price As Double

Sub new (By vale pid As integer, By val pname As string, By

val price As Double)

Me._Product Id = Pid

Me._Product Name = Pname

Me._Price = Price

End sub

Public Read only property Product Id as integer

Get

Return Me._Product Id

End Get

End property

Public Read only property Product Name as String

Get

Return Me._Product Name

End Get

End property

Public Read only property Price as Double

Get

Return Me._Price

End Get

End property

End class

Class ShowRoom

Public shared sub main ()

Dim tv As new product (101, “LG Golden Eye”,13500)

tv.As productName = “sekhar Diamond Eye” error since it

is read only

tv.price = 12000

Console.WriteLine (“Product Id: {0}”, tv.Product Id)

Console.WriteLine (“Product Name: {0}”, tv.ProductName)

Console.WriteLine (“Price: {0}”, tv.Price)

End sub

End class

Imports system

Module Property ‘‘Demo2

Class Product

Private – First Name As String

Private – Last Name As String

Public property First Name As String

Set (By val value As string)

Me._First Name = value

End set

Get

Return Me._First Name

End Get

End property

Public property Last Name As String

Set (By val value As string)

Me._Last Name = value

End set

Get

Return Me._Last Name

End Get

End property

End class

Sub main ()

Dim uname As New Name

uname.FirstName = “Shekar”

uname.LastName = “Srinivas”

Console.WriteLine (“user name: {0} {1}”,

uname.FirstName, uname.LastName)

End sub

Execute

 

Auto Implicit:

It is used to define a read and write property implicitly.

 

Syntax:

Public property PropertyName As Datatype

 

Note:

It is a feature introduced from VB.Net 4.0

 

New Module:

Auto Implicit property ‘‘Demo.vb

Imports system

Module Property ‘‘Demo2

Class Name

Public property First Name As String

Public property Last Name As String

End class

Sub main ()

Dim uname As New Name

uname.FirstName = “Shekar”

uname.LastName = “Srinivas”

Console.WriteLine (“user name: {0} {1}”,

uname.FirstName, uname.LastName)

End sub

End Module

The above two programs are same and gives the same output sekhar srinivas. In first program we write the code manually and in second program code is generated by technology (.Net). The second program works in only VB.Net 4.0. In the 1st program the data which is private has been published for using by means of “Property”. In the second program declaring of data types in private & published for using these by auto implicit keyword in a single line.

C: \Start notepad f.vb (first program)

C: \ vbc f.vb; o/p shekar srinivas

C: \ f.vb

C: \ Start notepad s.vb

C: \ vbc s.vb; C: \ s.vb

To know code after execution is C: \ Mdasm f.exe

 

 

Methods:

It is used to modulate the flow of the application.

 

Advantage:

  • Easy debugging [By writing logic in several methods rather than single method if a problem persists then we can rectify easily]
  • Reusability [if you write a method and there is another area od using that method we can call that methos]
  • Performance

VB.Net supports two types of methods:

  • Procedures or sub routines
  • Functions

The thing we can makw using procedures can also make with functions and vice versa. So in C, C++, Java we have only functions they feel functions & procedures are same. But VB.Net people feel that procedures and functions are different.

VB.Net supports strict english grammer and literal keywords like any other langauge. Procedure outcome of procedures and functions the thing we use is same but we select based on our requirements.

   

Procedures or subroutines:

It is a set of instructions which are used to perform a specific action or task.

Syntax:

[Modifiers] sub methodName ([arginfo])

‘Statement(s)’

- - - - - -

- - - - - -

End sub

 

Functions:

It is a set of instructions which is used to perform calculations, expression value and used to return a value.

 

Syntax:

Syntax:

[Modifier] function MethodName ([arginfo]) as Data type

‘Statement(s)’

- - - - - -

- - - - - -

return < Return value>

End function

 

In VB.Net main () method is a procedure but not function [so in VB.Net main () can’t return a value] constructor is also a procedure but not function.

Where argnfo in the above syntax is [By val|By ref] argName as Data type

 

By Val:

It is used to pass the value from the caller to the called.

 Screenshot_26

 

Note:

The default mode of passing the information in VB.Net is Byval

 

By Ref:

It is used to pass the reference of the variable from the caller to the called.

 

Note:

If any modifications are performed at the called then those changes will be reflected at the caller.

 

 

Screenshot_27

 

Observation:

Call By Value

Screenshot_28

 

Optional Parameters:

It is used to define a parameter as an optional parameter.

 

Note:

Optional parameters should always be defined as the last parameters within the method.

 

Screenshot_29

 

 

Note:

Optional parameters should be assigned with default values.

 

Param Arrays:

It is used to define a parameter present within the method as an array where array is a collection of related data type values.

 

Note:

Param arrays can be used only within the methods.

‘‘Demo on Methods

Import system

Module Methods ‘‘Demo

Class Calculator

Public Function Add (By Val a As Integer, By Val b As Integer) As                        Integer

Return a + b

End Function

Public Function Add (By Val a As Integer, By Val b As Integer) As                        Integer = 0)

Return Function

End Function

Public Function Add (By Val Param Array nos () As Integer) As Integer

Dim total As Integer = 0

For Each no As Integer In nos

Total = total + no

Next

Return total

End Function

Public sub Display (By Val result As Integer)

Console.WriteLine (Result: {0}, result)

End sub

End class

Sub main ()

Dim calc As New Calculator

Calc.Display (calc.Add (10, 20))

Calc.Display (calc.Add (1, 2, 3, 4, 5, 6, 7, 8, 9))

End sub

End Module

Execute 

 

Observation:

Whenever more than one method with the same name and different argument information is used then it is said to be method Overloading.

[If we want to determine more variables then we can use param arrays   if we don’t know how many arguments are passing we can use For Each instead of for Loop]

 

Inheritance:

It is used to reuse or redefine the existing class definition

Note: .Net supports only single inheritance

 

Syntax to inherit the class:

Class Derived class inherits Base class

member(s)    - - - - - - - -

End class

The member’s defined using public, protected; friend and protected friend modifiers at the base class can be accessed directly from the derived class.

The ex’s are customized applications purely for understanding the concept.

 

Screenshot_30

 

‘‘Demo on Inheritance

Imports system

Module Inheritance ‘‘Demo

Class Parents

Protected Friend sub MyProperty ( )

Console.WriteLine (“Use property for business”)

End sub

End class

(Class children inherits parents)

Class Children inherits Parents

End class

Sub main ( )

Dim c as New Children

C.MyProperty ( )

End sub

End Module