Advertisement
5_2007-2008 Miscellaneous #172202

Your First VB .NET OO Class

Here's a quick primer for developers who understand basic OO (Object Oriented) concepts and want to use that knowledge and start making using of VB .NET syntax right away.  This short example shows you how to use VB.NET syntax to: 1) declare properties (get/set), including read only properties 2) create a constructor and invoke it from a client 3) overloading of a constructor 4) inheritance

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
To set Form1 as a top-most form, do the following: 

#IF WIN32 THEN
Dim lResult as Long 
lResult = SetWindowPos (me.hWnd, HWND_TOPMOST, _
0, 0, 0, 0, FLAGS) 
#ELSE '16-bit API uses a Sub, not a Function
SetWindowPos me.hWnd, HWND_TOPMOST, _
0, 0, 0, 0, FLAGS
#END IF

To turn off topmost (make the form act normal again), do the following: 

#IF WIN32 THEN
Dim lResult as Long 
lResult = SetWindowPos (me.hWnd, HWND_NOTOPMOST, _
0, 0, 0, 0, FLAGS) 
#ELSE '16-bit API uses a Sub, not a Function
SetWindowPos me.hWnd, HWND_NOTOPMOST, _
0, 0, 0, 0, FLAGS
#END IF

If you don't want to force a window on top, which will prevent the user from seeing below it, but simply want to move a Window to the top for the user's attention, do this:

Form1.ZOrder
<p><font face="Verdana" size="2">Fire up VB.NET and create a new project. 
Then add a new item (of type class module) and name it car.vb.  Then paste
in the following code:<br>
<br>
</font></p>
<p><font size="2" face="Verdana"><b>Public Class clsCar<br>
<br>
    Private mintWheels As Integer<br>
<br>
    Public Property Wheels() As Integer<br>
        Get<br>
            Wheels = mintWheels<br>
        End Get<br>
<br>
        Set(ByVal Value As Integer)<br>
            mintWheels = Value<br>
        End Set<br>
    End Property<br>
<br>
    Public Sub New()<br>
        'a typical car defaults to 4 wheels<br>
        Wheels = 4<br>
    End Sub<br>
<br>
    Public Sub New(ByVal vintWheels As Integer)<br>
        'this is an overloaded contructor<br>
        Wheels = vintWheels<br>
    End Sub<br>
<br>
End Class<br>
<br>
Public Class clsFlyingCar : Inherits clsCar<br>
 'note the inherits keyword above so that clsFlyingCar<br>
 'inherits all the features of clsCar<br>
    Public ReadOnly Property FlyMe() As String<br>
        Get<br>
            FlyMe = "wheee!!!"<br>
        End Get<br>
    End Property</b></font></p>
<p><font size="2" face="Verdana"><b><br>
    Public Sub New()<br>
        'a flying car has no wheels<br>
        Wheels = 0<br>
    End Sub<br>
End Class</b></font></p>
<p><font size="2" face="Verdana">    Some interesting things to
notice is that unlike VB6, we can now declare more than one class in a single
form.  Also, the "<b>ReadOnly</b>" keyword is new...this
explicitly declares a property as read only...which makes it really easy for
other programmer's read.</font></p>
<p><font face="Verdana">   <font size="2"> Old time VB
programmers will give a hearty shout when they see the "<b>Inherits</b>"
keyword...this is what gives us full fledged inheritance!</font>  <font size="2">If
you look carefully, you'll also see a <b>Public Sub New</b> function...this is
how constructors are done in VB.NET.  By simply declaring a second <b>Public
Sub New</b> function with different input parameters, the constructor is
overloaded.</font></font></p>
<p><font size="2" face="Verdana">2) To exercise this class and show you how it
works, I've attached a small sample program that calls this class.</font></p>
<p><font size="2" face="Verdana">That's it!  VB.NET syntax is so simple its
painless and the new functionality is awesome, so enjoy!</font></p>
<p> </p>
<p> </p>
Original Comments (3)
Recovered from Wayback Machine