.NET Tips and Tricks

Blog archive

What's New in Visual Basic 14: Parameterless Constructors in Structures

While everyone using the .NET Framework creates Classes, not many developers create Structures. For those of you who are creating Structures, Visual Basic 14 has some good news for you: You can now give your structure a constructor that doesn't accept parameters (a default constructor). Formerly any constructor you added to a Structure had to accept at least one parameter.

Here's an example of a Structure with a default parameter:

Structure ProcessData
   Private ErrorCount As Integer
   Sub New()
      ErrorCount = -1
   End Sub
End Structure

If you want to have your default constructor actually execute, however, you must use the New keyword. This code will call the constructor and set ErrorCount to -1:

Dim pd As New ProcessData

This code will not:

Dim pd As ProcessData

and it will still run. Nifty, right?

Posted by Peter Vogel on 06/18/2015


comments powered by Disqus

Featured

Subscribe on YouTube