VB.Net Tutorial/Class Module/Object — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:55, 26 мая 2010
Assign string value to an Object variable
public class Test
public Shared Sub Main
Dim objMyModemSpeed As Object
objMyModemSpeed = "28.8"
Console.WriteLine(objMyModemSpeed)
Console.WriteLine(objMyModemSpeed * 1000 / 8)
objMyModemSpeed = Nothing
End Sub
End class
28.8 3600
Object lifetime
Imports System
Imports Microsoft.VisualBasic
Module MyModule
Sub Main()
Dim object1 As New MyClass1(1)
object1. CheckDisposal ()
object1.Dispose()
object1.CheckDisposal()
object1.Dispose()
Dim object2 As New MyClass1(2)
End Sub
End Module
Class MyClass1
Implements IDisposable
Private name As String
Private disposed As Boolean
Public Sub New(ByVal n As String)
name = n
disposed = False
Console.WriteLine("Constructor for {0}", name)
End Sub
Public Sub CheckDisposal()
If Not disposed
Console.WriteLine("{0} still in use", name)
End If
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
If Not disposed Then
Console.WriteLine("Dispose for {0}" & vbCrLf, name)
disposed = True
GC.SuppressFinalize(Me)
End If
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("Destructor for {0}", name)
Dispose()
End Sub
End Class
Constructor for 1 1 still in use Dispose for 1 Constructor for 2 Destructor for 2 Dispose for 2
Reference Equality
Imports System
Module Test
Sub Main()
Dim f as New Class1("A", 1)
Dim f2 as New Class1("A", 1)
Console.WriteLine(f2.Equals(f))
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
End Class
False True