IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesVB.NetOOP Details  Print This     

Cross Ref > OOP Details

By Mike Prestwood

VB.Net versus Access VBA: A side by side comparison between VB.Net and Access VBA.

 
OOP Details
 

More object oriented (OO) stuff.

Abstraction

[Other Languages] 

General Info: Abstract Class / Abstract Member

An abstract class member is a member that is specified in a class but not implemented. Classes that inherit from the class will have to implement the abstract member. Abstract members are a technique for ensuring a common interface with descendant classes. An abstract class is a class you cannot instantiate. A pure abstract class is a class with only abstract members.

Languages Focus

Abstraction is supported at various levels with each language. A language could enforce abstraction at the class level (either enforcing a no-instantiation rule or a only abstract members rule), and with class members (member methods and/or properties).

VB.Net:   MustInherit, MustOverride, Overrides

VB.Net supports abstract class members and abstract classes using the MustInherit and MustOverride modifiers.

An abstract class is indicated with a MustInherit modifier and is a class with one or more abstract members and you cannot instantiate an abstract class. However, you can have additional implemented methods and properties.

An abstract member is either a method (implicitly virtual), property, indexer, or event in an abstract class. You can add abstract members ONLY to abstract classes using the MustOverride keyword. Then you override it in a descendant class with Overrides.

Syntax Example:
Public MustInherit Class Cyborg
  Public MustOverride Sub Speak(ByVal pMessage As String)
End Class

Public Class Series600
  Inherits Cyborg

  Public Overrides Sub Speak(ByVal pMessage As String)
    MessageBox.Show(pMessage)
  End Sub
End Class
[Not specified yet. Coming...]




Class Helper

[Other Languages] 

A. In Dephi, class helpers allow you to extend a class without using inheritance. With a class helper, you do not have to create and use a new class descending from a class but instead you enhance the class directly and continue using it as you always have (even just with the DCU).

B. In general terms, developers sometimes use the term to refer to any class that helps out another class.

VB.Net:  "Class Helpers" Not Supported

However, developers sometimes use the term "class helper" to refer to code that helps out a class. Not truly the meaning we are using here, but you should be aware of the term's general usage.

Access VBA:  "Class Helpers" Not Supported




Code Contract

[Other Languages] 

A.k.a. Class Contract and Design by Contracts.

A contract with a method that must be true upon calling (pre) or exiting (post). A pre-condition contract must be true when the method is called. A post-condition contract must be true when exiting. If either are not true, an error is raised. For example, you can use code contracts to check for the validity of input parameters, and results

An invariant is also a code contract which validates the state of the object required by the method.

VB.Net:  "Code Contracts" Not Supported

Although not currently supported, there are plans for the next version of VS.Net and .Net using Requires and Ensures keywords. Look for code contracts in VS.Net 2010 and .Net 4.

If you code with VS.Net 2008 Pro or above and wish to implement contracts now, checkout the following download:

Access VBA:  "Code Contracts" Not Supported




Constructor

[Other Languages] 

General Info: Class Constructor

Constructors are called when you instantiate an object from a class. This is where you can initialize variables and put code you wish executed each time the class is created. When you initially set the member fields and properties of an object, you are initializing the state of the object. The state of an object is the values of all it's member fields and properties at a given time.

Languages Focus

What is the syntax? Can you overload constructors? Is a special method name reserved for constructors?

VB.Net:  "Constructors" New

In VB.Net, a constructor is called whenever a class or struct is created. A constructor is a sub named New. You can overload the constructor simply by adding two or more New subs with various parameters (overloaded constructors).

If you do not create a constructor, VB.Net will create an implicit constructor that initializes all member fields to their default values.

Constructors can execute at two different times. Static constructors are executed by the CLR before any objects are instantiated. Regular constructors are executed when you create an object.

You can invoke the parent constructor with MyBase.New.

Syntax Example:
Public Class Cyborg
Public CyborgName As String
  
  Public Sub New(ByVal pName As String)
CyborgName = pName
End Sub
End Class
Access VBA:  "Constructors" Class_Initialize

When an object instance is created from a class, Access VBA calls a special parameter-less sub named Class_Initialize. Since you cannot specify parameters for this sub, you also cannot overload it.

When a class is destroyed, Access VBA calls a special sub called Class_Terminate.

More Info / Comment  




Destructor

[Other Languages] 

General Info: Class Destructor

A special class method called when an object instance of a class is destroyed. With some languages they are called when the object instance goes out of scope, with some languages you specifically have to call the destructor in code to destroy the object, and others use a garbage collector to dispose of object instances at specific times.

Desctructors are commonly used to free the object instance but with languages that have a garbage collector object instances are disposed of when appropriate. Either way, destructors or their equivalent are commonly used to free up resources allocated in the class constructor.

Languages Focus

Are object instances freed with a garbage collector? Or, do you have to destroy object instances.

VB.Net:  "Finalizer" Finalize()

In VB.Net you cannot explicitly destroy a managed�object. Instead, the .Net Framework's garbage collector (GC) takes care of destroying all objects. The GC destroys the objects only when necessary. Some situations of necessity are when memory is exhausted or you explicitly call the System.GC.Collect() method. In general, you never need to call System.GC.Collect().

In .Net, a finalizer is used to free non-managed objects such as a file or network resource. In VB.Net, a finalizer is an overridden sub called Finalize. Because you don't know when the garbage collector will call your finalizer, Microsoft recommends you implement the IDisposable interface for non-managed resources and call it's Dispose() method at the appropriate time.

Syntax Example:
Class Cyborg
� Protected Overrides Sub Finalize()
��� 'Free non-managed resources here.
��� MyBase.Finalize()
� End Sub
End Class
Access VBA: 

When an object instance is destroyed, Access VBA calls a special parameter-less sub named Class_Terminate. For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.

To explicitly destroy an object, use Set YourClass = nothing.

When an object instance is created from a class, Access VBA calls a special sub called Class_Initialize.

More Info / Comment  




Inheritance-Multiple

[Other Languages] 
VB.Net:   Not Supported

VB.Net does not support multiple implementation inheritance. Each class can have only one parent class (a single inheritance path). In VB.Net, you can use multiple interface usage to design in a multiple class way horizontally in a class hierarchy.

More Info / Comment
Access VBA:   Not Supported




Interface

[Other Languages] 

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.

VB.Net:  "Interfaces" Interface, Implements

With VB.Net you define an interface with the Interface keyword and use it in a class with the Implements keyword. In the resulting class, you implement each property and method and add Implements Interface.Object to each as in:

Sub Speak(ByVal pSentence As String) Implements IHuman.Speak
  MessageBox.Show(pSentence)
End Sub
Syntax Example:
Public Interface IHuman
'Specify interface methods and properties here.
End Interface

Public Class Cyborg
Inherits System.Object
End Class

Public Class CyborgHuman
Inherits Cyborg
Implements IHuman
'Implement interface methods and properties here.
End Class
Access VBA:  "Interfaces"

Same as in VB6. Access VBA has limited support for interfaces. You can create an interface of abstract methods and properties and then implement them in one or more descendant classes. It's a single level implementation though (you cannot inherit beyond that). The parent interface class is a pure abstract class (all methods and properites are abstract, you cannot implement any of them in the parent class).

In the single level descendant class, you have to implement all methods and properties and you cannot add any. Your first line of code is Implements InterfaceName.

More Info / Comment




Overriding

[Other Languages] 

General Info: Method Overriding

Where you define or implement a virtual method in a parent class and then replace it in a descendant class.

When you decide to declare a method as virtual, you are giving permission to derived classes to extend and override the method with their own implementation. You can have the extended method call the parent method's code too.

In most OO languages you can also choose to hide a parent method. When you introduce a new implementation of the same named method with the same signature without overriding, you are hiding the parent method.

VB.Net:   Overridable, Overrides

In VB.Net, you specify a virtual method with the Overridable keyword in a parent class and extend (or replace) it in a descendant class using the Overrides keyword.

Use the base keyword in the descendant method to execute the code in the parent method, i.e. base.SomeMethod().

Syntax Example:
Public Class Robot
Public Overridable Sub Speak()
MessageBox.Show("Robot says hi")
End Sub
End Class
  
Public Class Cyborg
Inherits Robot
  
  Public Overrides Sub Speak()
MessageBox.Show("hi")
End Sub
End Class
[Not specified yet. Coming...]




Partial Class

[Other Languages] 

A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler. The compiler puts the class together at compile time and treats the final class or type as a single entity exactly the same as if all the source code was in a single location.

Languages Focus

For languages that have implemented partial classes, you need to know usage details and restrictions. Can you split a class into two or more files? Can you split a class within a source code file into two or more locations? What are the details of inheritance? Does it apply to interfaces as well?

VB.Net:  "Partial Classes" Partial

VB.Net supports both partial classes and partial methods.

Syntax Example:
Partial Public Class Cyborg
Inherits System.Object
End Class
Access VBA:  "Partial Classes" Not Supported




Polymorphism

[Other Languages] 

A coding technique where the same named function, operator, or object behaves differently depending on outside input or influences. Usually implemented as parameter overloading where the same named function is overloaded with other versions that are called either with a different type or number of parameters. Polymorphism is a general coding technique and other specific implementations are common such as inheritance, operator overloading, and interfaces.

Languages Focus

Many languages support built-in polymorphism such as a "+" operator that can add both integers and decimals. The following documents the ability to implement developer defined polymorphism.

VB.Net: 

VB.Net supports the following types of polymorphism:

More Info / Comment
[Not specified yet. Coming...]




Prevent Derivation

[Other Languages] 

Languages Focus

How do you prevent another class from inheriting and/or prevent a class from overriding a member.

VB.Net:   NotInheritable, NotOverridable

With VB.Net, use the NotInheritable keyword to prevent a class from being inherited from and use the NotOverridable keyword to prevent a method from being overridden.

A method marked NotOverridable must override an ancestor method. If you mark a class NotInheritable, all members are implicitly not overridable so the NotOverridable keyword is not legal.

Syntax Example:
Public Class Machine
  Public Overridable Sub Speak(ByRef pSentence As String)
MessageBox.Show(pSentence)
End Sub
End Class
  
Public Class Robot
Inherits Machine
  Public NotOverridable Overrides Sub Speak(ByRef pSentence As String)
MessageBox.Show(pSentence)
End Sub
End Class
  
Public NotInheritable Class Cyborg
Inherits Robot
End Class
Access VBA:   Not Supported




Static Member

[Other Languages] 

General Info: Static Class / Static Member

A static member is a member you can have access to without instantiating the class into an object. For example, you can read and write static properties and call static methods without ever creating the class. Static members are also called class members (class methods, class properties, etc.) since they belong to the class and not to a specific object. A static class is a class that contains only static members. In the UML, these classes are described as utility classes.

Languages Focus

Languages that support static members usually at least support static member fields (the data). Some languages also support static methods, properties, etc. in which case the class member is held in memory at one location and shared with all objects. Finally, some languages support static classes which usually means the compiler will make sure a static class contains only static members.

VB.Net:  "Shared Members" Shared

VB.Net supports both static members and static classes (use the keyword Shared). You can add a static method, field, property, or event to an existing class.

You can designate a class as static and the compiler will ensure all methods in that class are static. You can add a constructor to a static class to initialize values.

The CLR automatically loads static classes with the program or namespace.

Syntax Example:
Public Shared Function MySharedFunction() As Integer
  MySharedFunction = 12345
End Function
Access VBA:  "Static Members" Not Supported




Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]