Object casting : Cast « Language Basics « VB.Net






Object casting

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


           
       








Related examples in the same category