Perhaps I should first start out with a few little tips of terminology that I use.
Public Class
SlideBox
Public BoxHeight, BoxWidth,
BoxLength As Integer
'Set these when you want
Private mbox
As Integer
Private IsClosed
As Boolean
'These are private and used for
returning data here
Public Sub CloseBox()
IsClosed = True
'Closes the box
:}
End Sub
Public Property BoxNumber()
As Integer
Get
Return mbox
End Get
Set(ByVal
Value As Integer)
mbox = Value
End Set
End Property
'BoxNumber is set just like a regular
property.
Public ReadOnly Property
BoxVolume() As Integer
Get
Return BoxHeight * BoxWidth * BoxLength
End Get
'ReadOnly has
no Set statement.
End Property
Public Function BoxDensity(ByVal
Mass As Integer) As Single
Dim Vol
As Single = Convert.ToSingle(BoxHeight * BoxWidth *
BoxLength)
Return
Convert.ToSingle(Mass) / Vol
'Must convert
both items to Single.
End Function
End Class
This class will give the following members
if you use this instance from another class:
BoxHeight, BoxWidth, BoxLength, CloseBox, BoxNumber, BoxVolume, BoxDensity
Of these 7 members, BoxDensity and CloseBox are methods, denoted by the little
diamond.
Of course, you will also see a GetType method within MVS 20##, which you'll get in any class. They facilitate the conversions to and from object.
The other 5 are properties, and here's a little schematic of the class:
The gold properties there are the two private properties, mbox and IsClosed.
Now, suppose that I add two methods to the class like this:
Public
Shared Function Density(ByVal Mass As
Integer, ByVal Volume As
Integer) As Single
'D = M / V
Return
Convert.ToSingle(Mass) / Convert.ToSingle(Volume)
End Function
Public Shared Function Volume(ByVal
Mass As Integer, ByVal
Density As Single) As Integer
'Switch D with
V: V = M / D
Return
Convert.ToSingle(Mass) / Density
End Function
The Shared Function is accessible outside of the Class. For example:
Public Class
Ugh
Private X As
Single
Public Sub New()
X = SlideBox.Density(500, 200)
'X now contains
2.5.
End Sub
End Class
An overloaded method is one that lets you have multiple distinct parameter sets. Suppose that I wanted to calculate a potential energy for my box. U = mgh. I can easily make a function to do this:
Public
Const Gravity As Single = 9.81
Public Function PotentialEnergy(ByVal
Mass As Integer, ByVal
BoxAltitude As Single) As
Single
'If you must
know, I used Singles because g = 9.81 m/(s^2).
Return
Convert.ToSingle(Mass) * BoxAltitude * Gravity
End Function
Now, suppose I want to calculate the potential energy with respect to a certain height... if the box is sitting on a hill that is 10 high, but I want to find the energy the box will have if it went to 7 m...
Public
Overloads Function PotentialEnergy(ByVal
Mass As Integer, ByVal
BoxAltitude As Single) As
Single
'If you must
know, I used Singles because g = 9.81 m/(s^2).
Return
Convert.ToSingle(Mass) * BoxAltitude * Gravity
End Function
Public Overloads Function PotentialEnergy(ByVal
Mass As Integer, ByVal
BoxAltitude As Single, ByVal
ReferPoint As Single) As
Single
Return
Convert.ToSingle(Mass) * Gravity * (BoxAltitude - ReferPoint))
End Function
And I can call it like this:
Public Class Ugh
Private X As
Single
Public Sub New()
Dim SB As
SlideBox = New SlideBox
X = SB.PotentialEnergy(50, 14.2)
'X contains
6965.1
X = SB.PotentialEnergy(50, 14.2, 9.2)
'X contains
2452.5
End Sub
End Class