【问题标题】:VB redeclare as different typeVB重新声明为不同的类型
【发布时间】:2015-08-28 15:47:36
【问题描述】:

我可以通过在代码中再次声明来更改变量类型吗?喜欢...

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

x.something1
x.something2
x.something3

【问题讨论】:

  • 没有。我建议你使用接口。
  • 请努力发布格式正确且语法正确的代码。您发布的内容甚至不会编译。 VB 没有EndIF 关键字。它有 End If(带空格)。

标签: vb.net declaration


【解决方案1】:

正如@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多