(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Class declaration with a method that has a parameter
Module YourClassTest
Sub Main()
Dim obj As New YourClass
obj.DisplayMessage("AAA")
End Sub " Main
End Module
Public Class YourClass
Public Sub DisplayMessage(ByVal courseName As String)
Console.WriteLine( _
"Welcome to " & vbCrLf & courseName & "!")
End Sub
End Class
Welcome to
AAA!
Class that contains instance variable and a property to get and set its value
Module YourClassTest
Sub Main()
Dim obj As New YourClass
Console.WriteLine("Initial course name is: " & obj.YourName & vbCrLf)
obj.YourName = "new Name"
obj.DisplayMessage()
End Sub " Mains
End Module
Public Class YourClass
Private yourNameValue As String " course name for this YourClass
" property YourName
Public Property YourName() As String
Get " retrieve yourNameValue
Return yourNameValue
End Get
Set(ByVal value As String) " set yourNameValue
yourNameValue = value " store the course name in the object
End Set
End Property " YourName
" display a welcome message to the YourClass user
Public Sub DisplayMessage()
Console.WriteLine("Welcome to " & YourName & "!")
End Sub " DisplayMessage
End Class
Initial course name is:
Welcome to new Name!
Class with a constructor to initialize its member field value
Module YourClassTest
Sub Main()
Dim obj1 As New YourClass("AAA")
Dim obj2 As New YourClass("BBB")
Console.WriteLine(obj1.YourName)
Console.WriteLine(obj2.YourName)
End Sub " Main
End Module
Public Class YourClass
Private yourNameValue As String " course name for this YourClass
" constructor initializes course name with String supplied as argument
Public Sub New(ByVal name As String)
YourName = name " initialize yourNameValue via property
End Sub " New
" property YourName
Public Property YourName() As String
Get " retrieve yourNameValue
Return yourNameValue
End Get
Set(ByVal value As String) " set yourNameValue
yourNameValue = value " store the course name in the object
End Set
End Property " YourName
Public Sub DisplayMessage()
Console.WriteLine("Welcome to " & YourName & "!")
End Sub " DisplayMessage
End Class " YourClass
AAA
BBB
Define class
Option Strict On
Imports System
Public Class YourClass
Public weight As Integer
End Class
Public Class Tester
Public Shared Sub Main( )
Dim testObject As New Tester( )
testObject.Run( )
End Sub
Public Sub Run( )
Dim firstInt As Integer = 5
Dim secondInt As Integer = firstInt
Console.WriteLine("firstInt: {0} secondInt: {1}", firstInt, secondInt)
secondInt = 7
Console.WriteLine("firstInt: {0} secondInt: {1}", firstInt, secondInt)
Dim obj As New YourClass( )
obj.weight = 5
Dim anotherObject As YourClass = obj
Console.WriteLine("obj: {0}, anotherObject: {1}", obj.weight, anotherObject.weight)
anotherObject.weight = 7
Console.WriteLine( _
"obj: {0}, anotherObject: {1}", obj.weight, anotherObject.weight)
End Sub
End Class
firstInt: 5 secondInt: 5
firstInt: 5 secondInt: 7
obj: 5, anotherObject: 5
obj: 7, anotherObject: 7
Define your own Time Class
Module Tester
Sub Main()
Dim time As New CTime() " call CTime constructor
Console.WriteLine("The initial universal times is: " & _
time.ToUniversalString() & vbCrLf & _
"The initial standard time is: " & _
time.ToStandardString())
time.SetTime(13, 27, 6) " set time with valid settings
Console.WriteLine("Universal time after setTime is: " & _
time.ToUniversalString() & vbCrLf & _
"Standard time after setTime is: " & _
time.ToStandardString())
time.SetTime(99, 99, 99) " set time with invalid settings
Console.WriteLine("Universal time: " & time.ToUniversalString() & _
vbCrLf & "Standard time: " & time.ToStandardString())
End Sub
End Module
Class CTime
Inherits Object
Private mHour As Integer " 0 - 23
Private mMinute As Integer " 0 - 59
Private mSecond As Integer " 0 - 59
Public Sub New()
SetTime(0, 0, 0)
End Sub " New
Public Sub SetTime(ByVal hourValue As Integer, _
ByVal minuteValue As Integer, ByVal secondValue As Integer)
If (hourValue >= 0 AndAlso hourValue < 24) Then
mHour = hourValue
Else
mHour = 0
End If
If (minuteValue >= 0 AndAlso minuteValue < 60) Then
mMinute = minuteValue
Else
mMinute = 0
End If
If (secondValue >= 0 AndAlso secondValue < 60) Then
mSecond = secondValue
Else
mSecond = 0
End If
End Sub " SetTime
" convert String to universal-time format
Public Function ToUniversalString() As String
Return String.Format("{0}:{1:D2}:{2:D2}", _
mHour, mMinute, mSecond)
End Function " ToUniversalString
" convert to String in standard-time format
Public Function ToStandardString() As String
Dim suffix As String = " PM"
Dim format As String = "{0}:{1:D2}:{2:D2}"
Dim standardHour As Integer
If mHour < 12 Then
suffix = " AM"
End If
If (mHour = 12 OrElse mHour = 0) Then
standardHour = 12
Else
standardHour = mHour Mod 12
End If
Return String.Format(format, standardHour, mMinute, _
mSecond) & suffix
End Function
End Class
The initial universal times is: 0:00:00
The initial standard time is: 12:00:00 AM
Universal time after setTime is: 13:27:06
Standard time after setTime is: 1:27:06 PM
Universal time: 0:00:00
Standard time: 12:00:00 AM
Implement an interface
Class YourClass
Implements IFormattable
Public Value As String
Public Overridable Overloads Function ToString(ByVal _
Format As String, ByVal Provider As IFormatProvider) _
As String Implements IFormattable.ToString
ToString = Value
End Function
Public Sub New(ByVal Value As String)
Me.Value = Value
End Sub
End Class
Module Module1
Sub Main()
Dim A As New YourClass("Hello VB.Net World")
Dim S As New String("Hello")
End Sub
End Module
Overrides equals
Imports System
Module Test
Sub Main()
Dim f as New Class1("Visual Basic", 1)
Dim f2 as New Class1("Visual Basic", 1)
Console.WriteLine(f2.Equals(f)) "True!
f = f2
Console.WriteLine(f2.Equals(f))
End Sub
End Module
Public Class Class1
Private Name as String
Private Value as Integer
Public Sub New(Name as String, Value as Integer)
Me.Name = Name
Me.Value = Value
End Sub
Public Overrides Function ToString() as String
Return(Name & " has the value " & Value)
End Function
Public Overrides Overloads Function Equals(Obj as Object) as Boolean
"Value equality test
If Not IsNothing(Obj)
If TypeOf Obj is Class1 then
If CType(Obj, Class1).Name = Me.Name and CType(Obj, Class1).Value = Me.Value then
Return True
End If
End If
End If
Return False
End Function
End Class
True
True
Overrides ToString
Imports System
Module Test
Sub Main()
Dim f as New Class1("A", 1)
Dim f2 as New Class1("B", 2)
Console.WriteLine(f)
Console.WriteLine(f2)
End Sub
End Module
Public Class Class1
Private Name as String
Private Value as Integer
Public Sub New(Name as String, Value as Integer)
Me.Name = Name
Me.Value = Value
End Sub
Public Overrides Function ToString() as String
Return(Name & " has the value " & Value)
End Function
End Class
A has the value 1
B has the value 2
Structure and Class assignment: by reference or by value
public class Test
public Shared Sub Main
Dim cperson1 As New CPerson
Dim cperson2 As CPerson
cperson1.FirstName = "Alice"
cperson2 = cperson1
cperson2.FirstName = "Ben"
Console.WriteLine(cperson1.FirstName & ", " & cperson2.FirstName)
Dim sperson1 As New SPerson
Dim sperson2 As SPerson
sperson1.FirstName = "Alice"
sperson2 = sperson1
sperson2.FirstName = "Ben"
Console.WriteLine(sperson1.FirstName & ", " & sperson2.FirstName)
End Sub
End class
Public Class CPerson
Public FirstName As String
Public LastName As String
End Class
Public Structure SPerson
Public FirstName As String
Public LastName As String
End Structure
Ben, Ben
Alice, Ben