Home > Articles > Programming > Windows Programming

VB.NET's Object Oriented Features

In this sample chapter, Dan Fox discusses the features of VB.NET that allow it to be a fully OO language.
This sample chapter is excerpted from Building Distributed Applications with Visual Basic.NET, by Dan Fox.
This chapter is from the book

One of the biggest .NET concepts that existing corporate VB developers and those moving from ASP need to get a handle on is the thoroughly object-oriented nature of VB.NET. Despite the addition of class modules in VB 4.0 and the enhanced ability to create COM components in VB 5.0 and 6.0, VB remained primarily a procedural language, and many developers continue to use it as such. This is not to say that you couldn't use some object-oriented techniques with VB 6.0 (see Chapter 15 of my book Pure Visual Basic for more information). However, what OO features you could use were derived from COM and were therefore limited, and the VB language itself did not contain the keywords necessary to enable a truly OO implementation.

In VB.NET, this changes with the inclusion of keywords that directly support OO features implemented by the common language runtime. VB.NET derives these features from the CTS that, as discussed in Chapter 1, "The Microsoft .NET Architecture," uses System.Object as the ancestor or root of all types in VS.NET. As a result, VB cannot help but be object-oriented to the core, and because the CLR uses the concept of classes to expose functionality, VB.NET developers, unlike in previous versions, cannot avoid the use of classes. Finally, the architecture of the Services Framework mandates that developers use inheritance and polymorphism (which I will discuss later in the chapter) to take advantage of its services effectively. In other words, to take advantage of the power of the CTS to be productive and to use system services provided by the Services Framework, a good understanding of OO is required.

The addition of OO features in VB.NET is both a good and bad phenomenon. First, it is positive because the language is more powerful and as a result you can express more sophisticated designs in your solutions. And for developers like me who have always appreciated VB, this increased power also gives it a more professional air and serves to extend the population of developers who use it. However, this increase in power also comes with responsibility. Designing solutions that use inheritance, polymorphism, and encapsulation requires more discipline and knowledge on the part of the developer. For that reason, developers with formal computer science backgrounds or those willing to learn these concepts will probably feel the most comfortable with the changes. Not to worry, though, VB.NET extends the language in many ways that will feel quite natural so that everyone can exploit these features.

In this chapter, I'll discuss the features of VB.NET that allow it to be a fully OO language.

OO Terminology

Before moving to the language syntax, let's formally define the key OO concepts and terms that will be used in this chapter beginning with encapsulation, polymorphism, and inheritance.

Encapsulation

Encapsulation means that an object can hide its internal data structures from consumers of the object. Therefore, all of the object's internal data is manipulated through members (methods, properties, events, fields) of the object, rather than through direct references.

The primary benefits of encapsulation are maintainability and reusability. Code that takes advantage of encapsulation is more maintainable because consumers of the code work with the object through its public members. With a fully encapsulated object, for example, code outside the object cannot directly change a variable declared inside the object. By shutting off this direct access, fewer bugs are introduced because consumers of the object cannot inadvertently change the state of an object at run-time.

Abstracting the internal data of the object from consumers also leads to greater reusability. This follows because encapsulation leads to fewer dependencies between the consumer and the class and fewer dependencies is a prerequisite for creating reusable software.

Polymorphism

The second characteristic of OO systems is polymorphism. This concept is defined as the ability to write code that treats objects as if they were the same when in fact they are different. In other words, polymorphism allows you to write code that is generic across a set of objects that provide the same public members. Underneath the covers, each object might be implemented differently. However, as far as the consumer is concerned, each object looks the same and can be treated as such. In VB.NET, polymorphism can be created using both classes and interfaces.

The benefits of polymorphism revolve around the central fact that consumers of objects do not have to be aware of how the object performs its work, only that it does so through a specific set of members. This makes writing code that uses objects simpler by allowing the code to treat the object as if it were a black box, which leads to increased maintainability. Along the same lines, polymorphism allows you to write less code because each individual object does not have to be dealt with separately. Finally, polymorphism lends itself to writing code that can be reused because it will not be specific to a particular object.

Inheritance

The final OO concept is inheritance. Inheritance allows objects to share their interfaces (the definition of their members) and/or implementation in a hierarchy. For example, Tyrannosaurus and Velociraptor objects might be derived or inherited from a more generic Theropod object. All three objects share a basic set of members and, possibly, behaviors, such as carnivorousness, although the descendant objects might also include additional members or override members of Theropod. Inheritance allows objects to become more specific further down the hierarchy by adding additional members. In a nutshell, inheritance allows objects to reuse features (either their definition or their code) of other objects to which they are naturally related. The primary benefit of inheritance is, thus, reuse.

Obviously, inheritance and polymorphism are closely related, and, in fact, inheritance is what makes polymorphism possible in OO designs. It is always the case that objects that are in an inheritance relationship can be treated polymorphically. For example, if the Velociraptor object is inherited from the Theropod object, any consumer that is designed to work with Theropod objects will also work with Velociraptor objects.

VB.NET developers can benefit from inheritance in two ways: through interface inheritance and implementation inheritance. Interface inheritance allows only the definition of the object to be reused, whereas implementation inheritance allows the actual code written for the ancestor object (and its ancestors all the way down the line) to be reused. Which one to use is a design decision as mentioned in Chapter 1 and discussed more fully in this chapter.

NOTE

If developers wanted to use a form of implementation inheritance in previous versions of VB, they had to design classes to take advantage of the concepts of containment and delegation. Basically, these concepts mean that a class accessible by a consumer will contain a private instance of a second class and delegate its services to the consumer through its own interface. Containment and delegation are familiar terms (along with aggregation) to COM programmers.

More OO Terms

Familiarity with the following OO terms will be useful in this discussion.

  • Class—The fundamental unit of code reuse. Implemented with the Class statement in VB.NET. Classes can be inherited from and nested, and can contain members.

  • Base class—A class that serves as the ancestor for an inherited class, although this usually means the direct ancestor. VB.NET uses the MyBase keyword to refer to the direct ancestor. As a group, the ancestors are referred to as the base classes. The base class at the top of the hierarchy is referred to as the concrete base class.

  • Abstract base class—A base class that cannot be instantiated at run-time and serves only as the template for derived classes. In VB.NET, this is accomplished using the MustInherit keyword.

  • Interface—A well-defined collection of members along with their signatures. Interfaces provide no implementation. They are created in VB.NET with the Interface keyword.

  • Members—The methods, properties, events, and fields that make up an interface or class definition.

  • Methods—Named blocks of code within a class that can accept arguments and might return a value. VB.NET supports both Sub methods that do not return a value and Function methods that do.

  • Properties—Data exposed by a class and manipulated through code that runs when the property is set to a value or retrieved. In VB.NET, property creation is unified in a single Property statement.

  • Fields—Data exposed by a class directly to the consumer. A field represents a data location and therefore, its access is not abstracted. Fields are created in VB.NET using public variables within a class.

  • Events—Notifications fired by a class and captured by a consumer. In VB.NET, the Event and RaiseEvent statements are used to declare and raise events in addition to the use of delegates.

  • Overloading—There are two forms of overloading. The first is that a class can contain multiple definitions of the same method, each with different arguments (signatures). The consumer can then choose which method to call. The second is that derived classes can overload methods in a base class to alter the signature of the method. VB.NET uses the concepts of optional parameters, parameter arrays (paramarrays), and the Overloads keyword to implement both forms.

  • Overriding—Overriding a method means that a derived class can implement its own functionality for a method or property. VB.NET uses the Overrides, Overridable, NotOverridable, and MustOverride keywords to specify how methods and properties might or might not be overridden.

  • Static member—A member that is exposed by the base class, is not allowed to be overridden in a descendant, is available to all instances of the class, and is available even before the instance is created. This is also referred to as a shared member and is implemented with the Shared keyword.

  • Virtual member—The opposite of a static member, also referred to as an instance member. This refers to a method that can be overridden in a descendant class. By default, all methods and properties in VB.NET can be made virtual by including the Overridable keyword.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020