VB.Net/Language Basics/Cast — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Object casting
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim A As String
Dim myA As New A()
Dim myB As New B()
Dim myAReference As A
Dim myBReference As B
Dim myIReference As MyInterface
Console.WriteLine("Same Type Conversion")
myAReference = myA
Console.WriteLine("Base type - implicit")
myAReference = myB
Console.WriteLine("Implemented interface - implicit")
myIReference = myA
myA = CType(myIReference, A)
Try
myB = CType(myIReference, B)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
End Class
Interface MyInterface
Sub MyInterfaceFunction()
End Interface
Class A
Implements MyInterface
Protected Overridable Sub MyFunction()
End Sub
Public Sub MyPublicFunction()
End Sub
Public Sub MyInterfaceFunction() Implements MyInterface.MyInterfaceFunction
End Sub
End Class
Class B
Inherits A
End Class