加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:12:38 所属栏目:百科 来源:网络整理
导读:关于#If然后#Else( https://msdn.microsoft.com/en-us/library/tx6yas69.aspx)的MSDN文章为我提供了条件编译条件语句的基础知识. 我碰巧有更长的声明列表,必须根据多个平台进行不同的初始化.我是否需要在编译时使用#ElseIf,或者是否也有#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.这个名字甚至可以保持不变……

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读