VB重新声明为不同类型

时间:2015-08-28 15:47:37

标签: vb.net declaration

我可以通过在代码中再次声明它来更改变量类型。像...

Dim x As New DEV_CLASS
If environment = "UAT" Then
   Dim x As New UAT_CLASS
End If

x.something1
x.something2
x.something3

1 个答案:

答案 0 :(得分:2)

正如@TyCobb指出的那样,使用一个接口

 Dim x As MyInterface

 If environment = "UAT" Then
     x = New UAT_CLASS
 Else
     x = New DEV_CLASS
     'DirectCast(x, DEV_CLASS).SomeOtherDevMethod()
 End If

 x.Method1()
 x.Method2()

类和接口定义:

Public Interface MyInterface

    Sub Method1()
    Sub Method2()

End Interface

Public Class DEV_CLASS
    Implements MyInterface

    Public Sub Method1() Implements MyInterface.Method1

    End Sub

    Public Sub Method2() Implements MyInterface.Method2

    End Sub

    Public Sub SomeOtherDevMethod()

    End Sub

End Class

Public Class UAT_CLASS
    Implements MyInterface

    Public Sub Method1() Implements MyInterface.Method1

    End Sub

    Public Sub Method2() Implements MyInterface.Method2

    End Sub

End Class