本文整理汇总了VB.NET中System.Boolean结构体的典型用法代码示例。如果您正苦于以下问题:VB.NET Boolean结构体的具体用法?VB.NET Boolean怎么用?VB.NET Boolean使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
在下文中一共展示了Boolean结构体的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
Module Example
Public Sub Main()
Dim raining As Boolean = False
Dim busLate As Boolean = True
Console.WriteLine("It is raining: {0}", raining)
Console.WriteLine("The bus is late: {0}", busLate)
End Sub
End Module
输出:
It is raining: False The bus is late: True
示例2: Example
Module Example
Public Sub Main()
Dim raining As Boolean = False
Dim busLate As Boolean = True
Console.WriteLine("It is raining: {0}",
If(raining, "Yes", "No"))
Console.WriteLine("The bus is late: {0}",
If(busLate, "Yes", "No"))
End Sub
End Module
输出:
It is raining: No The bus is late: Yes
示例3: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim cultureNames() As String = { "", "en-US", "fr-FR", "ru-RU" }
For Each cultureName In cultureNames
Dim value As Boolean = True
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
Dim formatter As New BooleanFormatter(culture)
Dim result As String = String.Format(formatter, "Value for '{0}': {1}", culture.Name, value)
Console.WriteLine(result)
Next
End Sub
End Module
Public Class BooleanFormatter
Implements ICustomFormatter, IFormatProvider
Private culture As CultureInfo
Public Sub New()
Me.New(CultureInfo.CurrentCulture)
End Sub
Public Sub New(culture As CultureInfo)
Me.culture = culture
End Sub
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
If formatType Is GetType(ICustomFormatter) Then
Return Me
Else
Return Nothing
End If
End Function
Public Function Format(fmt As String, arg As Object,
formatProvider As IFormatProvider) As String _
Implements ICustomFormatter.Format
' Exit if another format provider is used.
If Not formatProvider.Equals(Me) Then Return Nothing
' Exit if the type to be formatted is not a Boolean
If Not TypeOf arg Is Boolean Then Return Nothing
Dim value As Boolean = CBool(arg)
Select culture.Name
Case "en-US"
Return value.ToString()
Case "fr-FR"
If value Then
Return "vrai"
Else
Return "faux"
End If
Case "ru-RU"
If value Then
Return "верно"
Else
Return "неверно"
End If
Case Else
Return value.ToString()
End Select
End Function
End Class
输出:
Value for '': True Value for 'en-US': True Value for 'fr-FR': vrai Value for 'ru-RU': верно
示例4: Example
Module Example
Public Sub Main()
Dim byteValue As Byte = 12
Console.WriteLine(Convert.ToBoolean(byteValue))
Dim byteValue2 As Byte = 0
Console.WriteLine(Convert.ToBoolean(byteValue2))
Dim intValue As Integer = -16345
Console.WriteLine(Convert.ToBoolean(intValue))
Dim longValue As Long = 945
Console.WriteLine(Convert.ToBoolean(longValue))
Dim sbyteValue As SByte = -12
Console.WriteLine(Convert.ToBoolean(sbyteValue))
Dim dblValue As Double = 0
Console.WriteLine(Convert.ToBoolean(dblValue))
Dim sngValue As Single = .0001
Console.WriteLine(Convert.ToBoolean(sngValue))
End Sub
End Module
输出:
True False True True True False True
示例5: Example
Module Example
Public Sub Main()
Dim flag As Boolean = true
Dim byteValue As Byte
byteValue = Convert.ToByte(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, byteValue,
byteValue.GetType().Name)
byteValue = CByte(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, byteValue,
byteValue.GetType().Name)
Dim sbyteValue As SByte
sbyteValue = Convert.ToSByte(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, sbyteValue,
sbyteValue.GetType().Name)
sbyteValue = CSByte(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, sbyteValue,
sbyteValue.GetType().Name)
Dim dblValue As Double
dblValue = Convert.ToDouble(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, dblValue,
dblValue.GetType().Name)
dblValue = CDbl(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, dblValue,
dblValue.GetType().Name)
Dim intValue As Integer
intValue = Convert.ToInt32(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, intValue,
intValue.GetType().Name)
intValue = CInt(flag)
Console.WriteLine("{0} -> {1} ({2})", flag, intValue,
intValue.GetType().Name)
End Sub
End Module
输出:
True -> 1 (Byte) True -> 255 (Byte) True -> 1 (SByte) True -> -1 (SByte) True -> 1 (Double) True -> -1 (Double) True -> 1 (Int32) True -> -1 (Int32)
示例6: Example
Module Example
Public Sub Main()
Dim values() As String = { Nothing, String.Empty, "True", "False",
"true", "false", " true ",
"TrUe", "fAlSe", "fa lse", "0",
"1", "-1", "string" }
' Parse strings using the Boolean.Parse method.
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
Console.WriteLine()
' Parse strings using the Boolean.TryParse method.
For Each value In values
Dim flag As Boolean = False
If Boolean.TryParse(value, flag)
Console.WriteLine("'{0}' --> {1}", value, flag)
Else
Console.WriteLine("Cannot parse '{0}'.", value)
End If
Next
End Sub
End Module
输出:
Cannot parse a null string. Cannot parse ''. True' --> True False' --> False true' --> True false' --> False true ' --> True TrUe' --> True fAlSe' --> False Cannot parse 'fa lse'. Cannot parse '0'. Cannot parse '1'. Cannot parse '-1'. Cannot parse 'string'. Unable to parse '' Unable to parse '' True' --> True False' --> False true' --> True false' --> False true ' --> True TrUe' --> True fAlSe' --> False Cannot parse 'fa lse'. Unable to parse '0' Unable to parse '1' Unable to parse '-1' Unable to parse 'string'
示例7: Example
Module Example
Public Sub Main()
Dim values() As String = { "09", "12.6", "0", "-13 " }
For Each value In values
Dim success, result As Boolean
Dim number As Integer
success = Int32.TryParse(value, number)
If success Then
' The method throws no exceptions.
result = Convert.ToBoolean(number)
Console.WriteLine("Converted '{0}' to {1}", value, result)
Else
Console.WriteLine("Unable to convert '{0}'", value)
End If
Next
End Sub
End Module
输出:
Converted '09' to True Unable to convert '12.6' Converted '0' to False Converted '-13 ' to True
示例8: Example
Module Example
Public Sub Main()
Dim hasServiceCharges() As Boolean = { True, False }
Dim subtotal As Decimal = 120.62d
Dim shippingCharge As Decimal = 2.50d
Dim serviceCharge As Decimal = 5.00d
For Each hasServiceCharge In hasServiceCharges
Dim total As Decimal = subtotal + shippingCharge +
If(hasServiceCharge, serviceCharge, 0)
Console.WriteLine("hasServiceCharge = {1}: The total is {0:C2}.",
total, hasServiceCharge)
Next
End Sub
End Module
输出:
hasServiceCharge = True: The total is $128.12. hasServiceCharge = False: The total is $123.12.
示例9: Example
Module Example
Public Sub Main()
Dim flags() As Boolean = { True, False }
For Each flag In flags
' Get binary representation of flag.
Dim value As Byte = BitConverter.GetBytes(flag)(0)
Console.WriteLine("Original value: {0}", flag)
Console.WriteLine("Binary value: {0} ({1})", value,
GetBinaryString(value))
' Restore the flag from its binary representation.
Dim newFlag As Boolean = BitConverter.ToBoolean( { value }, 0)
Console.WriteLine("Restored value: {0}", flag)
Console.WriteLine()
Next
End Sub
Private Function GetBinaryString(value As Byte) As String
Dim retVal As String = Convert.ToString(value, 2)
Return New String("0"c, 8 - retVal.Length) + retVal
End Function
End Module
输出:
Original value: True Binary value: 1 (00000001) Restored value: True Original value: False Binary value: 0 (00000000) Restored value: False
示例10: Example
' 导入命名空间
Imports System.IO
Imports System.Threading
Module Example
Public Sub Main()
' Initialize flag variables.
Dim isRedirected, isBoth As Boolean
Dim fileName As String = ""
Dim sw As StreamWriter = Nothing
' Get any command line arguments.
Dim args() As String = Environment.GetCommandLineArgs()
' Handle any arguments.
If args.Length > 1 Then
For ctr = 1 To args.Length - 1
Dim arg As String = args(ctr)
If arg.StartsWith("/") OrElse arg.StartsWith("-") Then
Select Case arg.Substring(1).ToLower()
Case "f"
isRedirected = True
If args.Length < ctr + 2 Then
ShowSyntax("The /f switch must be followed by a filename.")
Exit Sub
End If
fileName = args(ctr + 1)
ctr += 1
Case "b"
isBoth = True
Case Else
ShowSyntax(String.Format("The {0} switch is not supported",
args(ctr)))
Exit Sub
End Select
End If
Next
End If
' If isBoth is True, isRedirected must be True.
If isBoth And Not isRedirected Then
ShowSyntax("The /f switch must be used if /b is used.")
Exit Sub
End If
' Handle output.
If isRedirected Then
sw = New StreamWriter(fileName)
If Not IsBoth Then
Console.SetOut(sw)
End If
End If
Dim msg As String = String.Format("Application began at {0}", Date.Now)
Console.WriteLine(msg)
If isBoth Then sw.WriteLine(msg)
Thread.Sleep(5000)
msg = String.Format("Application ended normally at {0}", Date.Now)
Console.WriteLine(msg)
If isBoth Then sw.WriteLine(msg)
If isRedirected Then sw.Close()
End Sub
Private Sub ShowSyntax(errMsg As String)
Console.WriteLine(errMsg)
Console.WriteLine()
Console.WriteLine("Syntax: Example [[/f <filename> [/b]]")
Console.WriteLine()
End Sub
End Module