Imports System
'Fields hold the state of the object, and methods define the object's behavior.
Class cat
Private Shared instances As Integer = 0
Private weight As Integer
Private name As String
Public Sub New(ByVal name As String, ByVal weight As Integer)
instances += 1
Me.name = name
Me.weight = weight
End Sub
Public Shared Sub HowManyCats()
Console.WriteLine("{0} cats adopted ", instances)
End Sub
Public Sub TellWeight()
Console.WriteLine("{0} is {1} pounds", name, weight)
End Sub
End Class 'cat
Module Module1
Sub Main()
cat.HowManyCats()
Dim frisky As New cat("Frisky", 5)
frisky.tellWeight()
cat.HowManyCats()
Dim whiskers As New cat("Whiskers", 7)
whiskers.TellWeight()
whiskers.HowManyCats()
cat.HowManyCats()
Console.ReadLine()
End Sub
End Module
Here is another tutorial I worked on myself
Imports System
Public Class Employee
Private empID As Integer
Public Sub New(ByVal empID As Integer)
Me.empID = empID
End Sub
Public Overrides Function ToString() As String
Return empID.ToString()
End Function
End Class
Module Module1
Sub Main()
Dim empArray(5) As Employee
Dim counter As Integer = 0
Dim idNumber As Integer
While counter < 6
idNumber = Console.ReadLine()
empArray(counter) = New Employee(idNumber)
counter += 1
End While
Dim employeeInstance As Employee
For Each employeeInstance In empArray
Console.WriteLine("Employee ID# " & employeeInstance.ToString)
Next
Console.ReadLine()
End Sub
End Module
Here is another tutorial from the book by Jesse Liberty
Imports System
'The signature of a method is composed of its name and its parameter list
Public Class Time
'private member variables
Private yearYear As Integer
Private monthMonth As Integer
Private dateDate As Integer
Private hourHour As Integer
Private minuteMinute As Integer
Private secondSecond As Integer
'public accessor methods
Public Sub DisplayCurrentTime()
System.Console.WriteLine( _
"{0}/{1}/{2} {3}:{4}:{5}", _
monthMonth, dateDate, yearYear, hourHour, minuteMinute, secondSecond)
End Sub 'displayCurrentTime
'constructors
Public Sub New(ByVal dt As System.DateTime)
yearYear = dt.Year
monthMonth = dt.Month
dateDate = dt.Day
hourHour = dt.Hour
minuteMinute = dt.Minute
secondSecond = dt.Second
End Sub ' new
Public Sub New( _
ByVal aYear As Integer, _
ByVal aMonth As Integer, _
ByVal aDate As Integer, _
ByVal anHour As Integer, _
ByVal aMinute As Integer, _
ByVal aSecond As Integer)
Me.yearYear = aYear
Me.monthMonth = aMonth
Me.dateDate = aDate
Me.hourHour = anHour
Me.minuteMinute = aMinute
Me.secondSecond = aSecond
End Sub 'new
End Class 'time
Module Module1
Sub Main()
Dim currentTime As System.DateTime = System.DateTime.Now
Dim time1 As New Time(currentTime)
time1.DisplayCurrentTime()
Dim time2 As New Time(2005, 11, 18, 11, 3, 30)
time2.DisplayCurrentTime()
Console.ReadLine()
End Sub
End Module
Here is a tutorial about passing values on to subs using classes
Imports System
Public Class Tester
Public Sub run()
'declare a variable and initialize to 5
Dim theVariable As Integer = 5
'dipslay its value
Console.WriteLine("In Run. theVariable: {0}", theVariable)
'call a mthod & pass in the variable
Doubler(theVariable)
'return & display the value again
Console.WriteLine("Back in Run, theVariable: {0}", _
theVariable)
End Sub
Public Sub Doubler(ByVal param As Integer)
'display the value that was passed in
Console.WriteLine("In method1. received param: {0}", _
param)
'dobule the value
param *= 2
'display the dobuled value before returning
Console.WriteLine("Updated param. Returning new value: {0}", param)
End Sub
End Class 'tester
Module Module1
Sub Main()
Dim t As New Tester()
t.run()
Console.ReadLine()
End Sub
End Module
No comments:
Post a Comment