WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Object Oriented Programming for VB.NET - Part 1
Pages: 1, 2, 3

Listing 5: Moving the Main sub to the class itself


Class Employee

  Dim salary As Decimal = 40000
  Dim yearlyBonus As Decimal = 4000

  Public Sub PrintSalary()
    ' print the salary to the Console
    System.Console.Write(salary)
  End Sub


  Public Shared Sub Main()

    Dim employee As Employee
    employee = New Employee()
    employee.PrintSalary()

  End Sub

End Class



Note that System.Console.Write in the PrintSalary method means that you call the Write method of the Console class. Console is part of the System namespace. Namespaces are discussed next.

Namespaces

When you write a .NET program, you write classes and other types. To make your application more organized, you can group your classes into namespaces. This is also what Microsoft does with the .NET Framework class library. If you open the .NET Framework class library in the .NET Framework SDK documentation, you can see there are more than 80 namespaces. Important namespaces you will frequently use include System, System.IO, System.Drawing, System.Windows.Forms, etc.

For example, in the PrintSalary method in the Employee class we used the Console class in the System namespace.

If you will be using a namespace often, you can import a namespace so that you don't have to repeat it every time you want to use its member. For instance, you can rewrite the code in Listings 4 and 5 as shown below.

Listing 6: Importing a namespace


Imports System

Class Employee

  Dim salary As Decimal = 40000
  Dim yearlyBonus As Decimal = 4000

  Public Sub PrintSalary()
    ' print the salary to the Console
    Console.Write(salary)
  End Sub


  Public Shared Sub Main()
    Dim employee As Employee
    employee = New Employee()
    employee.PrintSalary()
  End Sub

End Class

Now, you can use the Console class in the PrintSalary method without mentioning the namespace because the namespace is already imported. Namespaces also allow you to have classes with the same name in different namespaces. To refer to a class correctly, it is common practice to mention the namespace in front of the class name. For example, the Console class in the System namespace is referred to as System.Console.

Access Types

In many cases, when you write a class, you will provide that class to other people so they can use its functionality, i.e., they can call methods of that class or access its fields. One of the great benefits of OOP is that you can restrict access to a class member. This means, you have full control over what you want to expose. You can write methods that can be called by other programmers, or you can have methods that are not accessible by anyone other than from inside the class itself.

VB.NET offers levels of accessibility, as follows:

  • Public. Public class members don't have access restrictions. You use the keyword Public in front of the class member to make it available publicly. For example, the PrintSalary method in the Employee class is a public method. It can be called from anywhere.
  • Private. Private class member can only be accessed from inside the class itself. Use the Private keyword to make a class member private.
  • Protected. A protected member is accessible to a derived class and from inside the class itself. Use the Protected keyword to make a member protected.
  • Friend. Members with the friend access restriction are accessible only within the program that contains the class declaration. Use the keyword Friend to make a member have friend restriction.
  • Protected friend. This is the union of protected and friend.

These different access types provide for information hiding in OOP. In other words, you can protect class members that you don't want to expose.

Pages: 1, 2, 3

Next Pagearrow