Module Module1
' MCTS70-536-1-1
' VB.NET
' Demonstration of a structure with
' an operator (+)
' an Override (toString)
'
Public Structure DemoStruct
Private _waarde As Integer
' --- Read and write initial & current value
Public Property Value() As Integer
Get
Return _waarde
End Get
Set(ByVal value As Integer)
_waarde = value
End Set
End Property
' --- Override de + OPERATOR, to SUBSTRACT the value in arg2 from the struct defined in arg1
Public Shared Operator +(ByVal Arg1 As DemoStruct, ByVal arg2 As Integer) As DemoStruct
Arg1.Value -= arg2
Return Arg1
End Operator
' --- Override the toString METHOD, to display * characters around the output
Public Overrides Function ToString() As String
Return String.Format("*{0}*", Value)
End Function
End Structure
Sub Main()
Dim x As New DemoStruct
x.Value = 10
For f As Integer = 1 To 10
Console.WriteLine(x.Value & " " & x.Value.ToString & " override: " & x.ToString)
x += 2 ' --- This will actualy substract 2, since we did an override of the + operator
x = x + 1 ' --- Same ! + is overriden and now acts as -
Next
' --- End run
Console.WriteLine(">>")
Console.ReadLine()
End Sub
End Modul