将枚举转换为布尔值的转换器类 [英] Converter Class which is convert enum to boolean

查看:151
本文介绍了将枚举转换为布尔值的转换器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi Friend

我将枚举转换为布尔值,如

枚举
abc 0
xyz 1
asd 2

使用vonverter类在boolena中转换这些值指导我plz

hi Friend

i wnat convert enum to boolean value like

enum
abc 0
xyz 1
asd 2

convert these value in boolena using vonverter class guide me plz

推荐答案

好吧,布尔型只能表示2个值,true和false,我可以看到如何转换一个枚举要布尔.

您仍然可以建立一些规则,并在ex发言.如果enum的值为0,则为false,否则为true.

函数将如下所示:
Well as Boolean can only represent 2 values, true and false, I can see how can you convert an enum to bool.

Still you can establish some rules and say at ex. that if value of enum is 0 than is false, otherwise it is true.

A function will look like this:
Public Function GetBoolFromEnum(e As MyEnum) As Boolean
    Return CInt(e) = 0
End Function

Enum MyEnum
    asdf = 1
    fdsa = 2
    tre = 3
End Enum




干杯




Cheers


好吧,

布尔值可以是TRUE或FALSE.

通常,枚举用于为名称赋予特定的值,并使代码更易于阅读.

阅读这些链接:

有关.NET枚举的提示 [ http://msdn.microsoft.com/en-us/library/8h84wky1 (v = vs.80).aspx [ http://www.dotnetperls.com/enum-vbnet [
Well,

A boolean value can be TRUE or FALSE.

Typically enums are used to give names a specific value and make the code easier to read.

Read those links:

Tips about .NET Enums[^]
http://msdn.microsoft.com/en-us/library/8h84wky1(v=vs.80).aspx[^]
http://www.dotnetperls.com/enum-vbnet[^]

Those are links to articles that speak about enums.

If you want to convert the values to boolean, then I would simply make something like
Public Enum MyEnum
    alfa = 1
    beta = 0
    gamma = 1
  End Enum


然后使用函数来区分1(TRUE)和0(FALSE).

祝你好运.


And then use a function to discriminate between 1 (TRUE) and 0 (FALSE).

Good luck.


可能会有所帮助.
1.
May be some help.
1.
result = Convert.ToBoolean(number) 



参考:
http://msdn.microsoft.com/en-us/library/h0b8dwce [ ^ ]

2.



Ref: http://msdn.microsoft.com/en-us/library/h0b8dwce[^]

2.

Module Example
   Public Sub Main()
      Dim values() As String = { Nothing, String.Empty, "True", "False", 
                                 "true", "false", "    true    ", "0", 
                                 "1", "-1", "string" }
      For Each value In values
         Try 
            Dim flag As Boolean = Boolean.Parse(value)
            Console.WriteLine("'{0}' --> {1}", value, flag)
         Catch e As ArgumentException
            Console.WriteLine("Cannot parse a null string.")
         Catch e As FormatException
            Console.WriteLine("Cannot parse '{0}'.", value)
         End Try          
      Next                                      
   End Sub 
End Module 
' The example displays the following output: 
'       Cannot parse a null string. 
'       Cannot parse ''. 
'       'True' --> True 
'       'False' --> False 
'       'true' --> True 
'       'false' --> False 
'       '    true    ' --> True 
'       Cannot parse '0'. 
'       Cannot parse '1'. 
'       Cannot parse '-1'. 
'       Cannot parse 'string'.



参考:
http://msdn.microsoft.com/en-us/library/system. boolean.parse.aspx#Y792 [ ^ ]



Ref:
http://msdn.microsoft.com/en-us/library/system.boolean.parse.aspx#Y792[^]


这篇关于将枚举转换为布尔值的转换器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆