April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

C# and VB Object-Oriented Programming in Visual Studio .NET

  • September 18, 2002
  • By Yan Locas, Erik Renaud
  • Send Email »
  • More Articles »

Object-Oriented Programming (OOP) is here to stay. It's a programming paradigm that emulates the human way of seeing things. Microsoft has pledged support for OOP by incorporating two major changes to the latest version of Visual Studio .NET. First, C# has been introduced as a brand new object-oriented language specifically designed for .NET. Second, Visual Basic has matured as a full-fledged Object-Oriented Programming (OOP) language.

This article will explore the significance of these changes to the Microsoft development community and offer tips for programming in VB.NET. Specific questions to be addressed include:

  1. What exactly is an object-oriented language?
  2. What OOP constructs can we expect to be writing in VB.NET and C#?
  3. Are there any tools or notations that can help us design OOP applications?
  4. What best practices can we apply to produce more efficient code and designs that promote reuse?
  5. What is the next big thing in OOP?

I. What Exactly Is an Object-Oriented Language?

OOP first appeared as a major implementation with SmallTalk in the early 1980's and has been formally managed by the Object Management Group (OMG) since 1989. OMG provides industry guidelines for object-oriented software development by supporting a Unified Modeling Language (UML) as a standard notation for modeling applications.

Today, many organizations have adopted UML as a standard for specifying, visualizing, constructing, and documenting the artifacts of software systems. The standard defines a set of diagrams that can be used to model different aspects of a system: use cases and requirements, business logic, dataflow, states and activities, static and dynamic views, and so forth.

One view, the class diagram (see the following example), represents the static structure of an application. That is, the things that exist (which will ultimately show up in your code), their internal structure, and their relationship to one another.

Each programming language has its own way to implement static structures. C# is a new language similar to Java and C++. If you're used to these languages, you shouldn't have any trouble getting into C# and its related OOP implementation details. On the other hand, if you're used to writing code in VB 3, 4, 5, or 6, you'll probably have a harder time getting into the full-fledged OOP world of VB.NET. Concepts such as inheritance, overloading, overriding, and namespaces are new to VB.NET, but the advantages they bring are not to be taken lightly.

In order to understand the power of OOP, consider, for example, form inheritance, a new feature of .NET that lets you create a base form that becomes the basis for creating more advanced forms. The new "derived" forms automatically inherit all the functionality contained in the base form. This design paradigm makes it easy to group common functionality and, in the process, reduce maintenance costs. When the base form is modified, the "derived" classes automatically follow suit and adopt the changes. The same concept applies to any type of object. Have a look at http://msdn.microsoft.com/library/en-us/vbcon/html/vbconFormInheritance.asp for more information on form inheritance.

II. What OOP Constructs Can We Expect to be Writing in VB.NET and C#?

This section outlines the following six constructs that help define the relationship between UML models and the accompanying implementation code in VB.NET code and C#. These include classes, interfaces, inheritance, overriding and overloading, aggregation, and composition as well as other associations.

Classes

In a UML static structure diagram, the class is the main element. The two other main elements are interfaces and associations. A class is said to have attributes and behaviors. An attribute is simply some piece of data that is bound to a class. Attributes are often referred to as class variables or properties. The behaviors are the actions that the class can perform. They are referred to as operations or methods (subs and functions in VB).

In OOP, classes are the actual containers for all of your coding. Classes are also held in another container called a package. Packages allow you to logically group the different classes that make up your system. You're allowed to insert a class of the same name in two different packages so that you can have, for example, the following result:

  • System.Database Invoice
  • System.Business.Invoice
  • System.GUI.Invoice

As you can see, there are three Invoice classes, each accomplishing something different (relating to an invoice) and each contained within its own package. This would be ideal in a three-tier system. Programming languages such as C# and VB.NET implement the notion of packages through namespaces.

C# was based entirely on classes from the ground up. VB programmers have been defining classes since version 5.0. Although you were able to use classes in previous VB releases, the classes were defined only as simple units. They could not take advantage of advanced concepts such as inheritance (see below). To use a class, you usually need to create an instance of it. Each instance of a class can hold its own (different) values for attributes. The behaviors will act upon that particular set of data.

Sometimes it's necessary to share an attribute (or a behavior) over each instance. In this case it is declared as static (underlined in UML notation). To access a static attribute or behavior you do not need to create a class instance. To understand how you could use static attributes or behaviors, imagine a class named LoggedOnUser, which has a static attribute named TotalLoggedOnUsers. When such a class is created, it would increment the static attribute, and when the class is destroyed, it would decrement the attribute.

The following UML diagram shows a class and its associated attributes and operations:

Here's what the code looks like in VB and C#:

VB.NET Code Snippet C# Code Snippet
Public Class Customer
    Public ID As Integer
    Public name As String
    Public phone As String

    Public Function GetLastOrder() As
Order
           Return New Order()
    End Function

End Class
public class Customer
{
 public int ID;
 public string name;
 public string phone;

 public Order GetLastOrder()
 {
     return new Order();
 }
}

In this example, Customer is the name of the class. ID, Name and Phone are attributes of the class, and getLastOrder is the name of an operation that returns an object of type Order.





Page 1 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel