当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET FlagsAttribute类代码示例

本文整理汇总了VB.NET中System.FlagsAttribute的典型用法代码示例。如果您正苦于以下问题:VB.NET FlagsAttribute类的具体用法?VB.NET FlagsAttribute怎么用?VB.NET FlagsAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了FlagsAttribute类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Example

Module Example
   ' Define an Enum without FlagsAttribute.
   Enum SingleHue As Short
      None = 0
      Black = 1
      Red = 2
      Green = 4
      Blue = 8
   End Enum

   ' Define an Enum with FlagsAttribute.
   <Flags()> 
   Enum MultiHue As Short
      None = 0
      Black = 1
      Red = 2
      Green = 4
      Blue = 8
   End Enum

   Sub Main()
      ' Display all possible combinations of values.
      Console.WriteLine(
           "All possible combinations of values without FlagsAttribute:")
      For val As Integer = 0 To 16
         Console.WriteLine("{0,3} - {1:G}", val, CType(val, SingleHue))
     Next 
     Console.WriteLine()
     
     ' Display all combinations of values, and invalid values.
     Console.WriteLine( 
          "All possible combinations of values with FlagsAttribute:")
     For val As Integer = 0 To 16
        Console.WriteLine( "{0,3} - {1:G}", val, CType(val, MultiHue))
     Next 
   End Sub 
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:37,代码来源:FlagsAttribute

输出:

All possible combinations of values without FlagsAttribute:
0 - None
1 - Black
2 - Red
3 - 3
4 - Green
5 - 5
6 - 6
7 - 7
8 - Blue
9 - 9
10 - 10
11 - 11
12 - 12
13 - 13
14 - 14
15 - 15
16 - 16

All possible combinations of values with FlagsAttribute:
0 - None
1 - Black
2 - Red
3 - Black, Red
4 - Green
5 - Black, Green
6 - Red, Green
7 - Black, Red, Green
8 - Blue
9 - Black, Blue
10 - Red, Blue
11 - Black, Red, Blue
12 - Green, Blue
13 - Black, Green, Blue
14 - Red, Green, Blue
15 - Black, Red, Green, Blue
16 - 16

示例2: Main

<Flags()>
Public Enum PhoneService As Integer
   None = 0
   LandLine = 1
   Cell = 2
   Fax = 4
   Internet = 8
   Other = 16
End Enum

Module Example
   Public Sub Main()
      ' Define three variables representing the types of phone service
      ' in three households.
      Dim household1 As PhoneService = PhoneService.LandLine Or
                                       PhoneService.Cell Or
                                       PhoneService.Internet
      Dim household2 As PhoneService = PhoneService.None
      Dim household3 As PhoneService = PhoneService.Cell Or
                                       PhoneService.Internet

      ' Store the variables in an array for ease of access.
      Dim households() As PhoneService = { household1, household2,
                                           household3 }

      ' Which households have no service?
      For ctr As Integer = 0 To households.Length - 1
         Console.WriteLine("Household {0} has phone service: {1}",
                           ctr + 1,
                           If(households(ctr) = PhoneService.None,
                              "No", "Yes"))
      Next
      Console.WriteLine()
      
      ' Which households have cell phone service?
      For ctr As Integer = 0 To households.Length - 1
         Console.WriteLine("Household {0} has cell phone service: {1}",
                           ctr + 1,
                           If((households(ctr) And PhoneService.Cell) = PhoneService.Cell,
                              "Yes", "No"))
      Next
      Console.WriteLine()
      
      ' Which households have cell phones and land lines?
      Dim cellAndLand As PhoneService = PhoneService.Cell Or PhoneService.LandLine
      For ctr As Integer = 0 To households.Length - 1
         Console.WriteLine("Household {0} has cell and land line service: {1}",
                           ctr + 1,
                           If((households(ctr) And cellAndLand) = cellAndLand,
                              "Yes", "No"))
      Next
      Console.WriteLine()
      
      ' List all types of service of each household?'
      For ctr As Integer = 0 To households.Length - 1
         Console.WriteLine("Household {0} has: {1:G}",
                           ctr + 1, households(ctr))
      Next
      Console.WriteLine()
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:61,代码来源:FlagsAttribute

输出:

Household 1 has phone service: Yes
Household 2 has phone service: No
Household 3 has phone service: Yes

Household 1 has cell phone service: Yes
Household 2 has cell phone service: No
Household 3 has cell phone service: Yes

Household 1 has cell and land line service: Yes
Household 2 has cell and land line service: No
Household 3 has cell and land line service: No

Household 1 has: LandLine, Cell, Internet
Household 2 has: None
Household 3 has: Cell, Internet


注:本文中的System.FlagsAttribute类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。