vb.net – 我可以有条件地编译“Select Case”吗?

前端之家收集整理的这篇文章主要介绍了vb.net – 我可以有条件地编译“Select Case”吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
关于#If然后#Else( https://msdn.microsoft.com/en-us/library/tx6yas69.aspx)的MSDN文章为我提供了条件编译条件语句的基础知识.

我碰巧有更长的声明列表,必须根据多个平台进行不同的初始化.我是否需要在编译时使用#ElseIf,或者是否也有#Select Case选项?

解决方法

VB .Net中没有#Select Case指令( as pointed out by Icepickle)

根据Conditional Compilation,您可以定义编译常量#Const并测试它们以包含或排除代码块.

如果您有许多不同的体系结构/平台,也许最好为每个平台编写不同的文件,并使用常量检查保护文件

首先,您声明一个接口,以便其余代码始终能够找到它所需的内容

Public Interface IPlatformDependant
    Property Test1 As Integer
    'Define here all the parameters used by your application
End Interface

文件platform1.vb:

#If Platform = 1
    'The code for the first platform
    Public Class PlatformDependant
        Implements IPlatformDependant

        Public Property Test1 As Integer Implements IPlatformDependant.Test1

    End Class
#End If

文件platform2.vb:

#If Platform = 2
    'The code for the second platform
    Public Class PlatformDependant
        Implements IPlatformDependant

        Public Property Test1 As Integer Implements IPlatformDependant.Test1

    End Class
#End If

在项目设计器中,您可以定义所需的平台,并且一次只能使用一个类PlatformDependant.这个名字甚至可以保持不变……

这里的接口是可选的,但它确保您所有的类都实现了所需的方法.

猜你在找的VB相关文章