March 2, 2021
Hot Topics:

The Book of Visual Studio .NET - OOP in VB .NET Crash Course

  • By Developer.com Staff
  • Send Email »
  • More Articles »

Methods are the actions exposed by a class in the form of either functions or sub-procedures. Sub-procedures and functions both execute code on behalf of the calling application, but sub-procedures simply execute code while functions execute code, then return a value.

The .NET Framework provides at least two new changes to how you can use procedures. In Visual Basic, you could call a procedure without the use of parameters, including procedures that required no parameters at all. The .NET Framework requires parenthesis to follow all methods even when parameters are not required. For example:

  • Visual Basic 6 method call:

    intResult = DoSomething
  • Visual Basic .NET method call:

    intResult = DoSomething()

Another change is the addition of the Return keyword. When returning a value for a function in Visual Basic, you set the function's name equal to the value being returned. With Visual Basic .NET, you can se t the keyword Return equal to a value and the value will be returned with the function. This is very useful when making code more generic. For instance, you can easily cut and past a method's code without regard to another method's function name because the keyword Return is used for setting the method equal to a return value. Examples of the old versus new method for returning values of a function are:

  • Visual Basic 6 method call:

    Public Function DoSomething() as Int32
    DoSomething = 10
    End Function
  • Visual Basic .NET method call:

    Public Function DoSomething() as Int32
    Return = 10
    End Function
  • If you look closely at a function's supporting properties you will find that the Return keyword is used by default. You can set the function name equal to a given value.

    The third significant change is in how parameters are passed. Visual Basic passed a parameter value ByRef by default. The preferred method for passing parameter values is to explicitly define whether a value is passed by ByRef or ByVal. Finally, when using the Option keyword, you must define a default value similar to how C has worked for many years now.

    Declaration Options

    We have covered a few of the most common declaration methods including those that describe the scope of a property or method. Several description options will extend or restrict scope.

    Here is a list of the most commonly-used declaration options with brief descriptions of each:

  • Private: The Private keyword defines a variable or method as accessible only by code within the context of where the declaration occurred; outside code is not permitted access. Variables and methods defined as private are often referred to as member variables or methods, and commonly prefixed with an "m".

  • Public: The Public keyword declares a property or method as accessible by anyone within the calling application or within the class itself.

  • Friend: The Friend keyword defines a property or method as accessible by members within the class it is declared in.

  • Protected: The Protected keyword defines a property or method as accessible only by members of its class or by members of an inheriting class.

  • Default: A Default property is a single property of a class that can be set as the default. This allows developers that use your class to work more easily with your default property because they do not need to make a direct reference to the property. Default properties cannot be initialized as Shared or Private and all must be accepted at least on argument or parameter. Default properties do not promote good code readability, so use this option sparingly.

  • Overloads:The Overloads property allows a function to be described using deferent combinations of parameters. Each combination is considered a signature, thereby uniquely defining an instance of the method being defined. You can define a function with multiple signatures without using the keyword Overloads, but if you use the Overloads keyword in one, you must use it in all of the function's Overloaded signatures.

  • Shared:The Shared keyword is used in an inherited or base class to define a property or method as being shared among all instances of a given class. If multiple instances of a class with shared properties or methods are loaded, the shared properties or methods will provide the same data across each instance of the class. When one class alters the value for a shared property, all instances of that class will reflect the change. Shared properties of all instances of the class point to the same memory location.

  • Overridable:The Overridable keyword is used when defining a property or method of an inherited class, as overridable by the inheriting class.

  • Overides: The Overides keyword allows the inheriting class to disregard the property or method of the inherited class and implements its own code.

  • NotOverridable: The NotOverridable keyword explicitly declares a property or method as not overridable by an inheriting class, and all properties are "not overridable" by default. The only real advantage to using this keyword is to make your code more readable.

  • MustOverride: The MustOverride keyword forces the inheriting class to implement its own code for the property or method.

  • Shadows: The Shadows keyword works like the Overloads keyword except that with shadows we do not have to follow rules such as implementing the same signature. The Shadows keyword does not require the consent (override ability) of the inherited class to replace the property or method's implementation code. A method does not have to be defined as overridable for the Shadows keyword to work.

Page 3 of 7

This article was originally published on February 3, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date