Click here to Skip to main content
15,886,101 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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:
VB
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
 
Share this answer
 
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
VB
Public Enum MyEnum
    alfa = 1
    beta = 0
    gamma = 1
  End Enum

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

Good luck.
 
Share this answer
 
May be some help.
1.
VB
result = Convert.ToBoolean(number) 


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

2.
VB
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'.


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900