No boolean object in VB.NET? How to check if a boolean has been explicitely defined then?

L

Lucas Tam

In java for instance there's a way to use booleans as objects and not
as value types.
I would like to do the same in VB.NET so that I can check if the
boolean has been explicitely defined (is not Nothing).
But Boolean is a Structure in VB.NET (defined to False by default) and
not an Object so I'm afraid there's no way to do what I want without
any workaround.
Can you confirm that there's no object version of Boolean in VB.NET?


Nope, I don't believe there is a Boolean object.

And there should be a way for you to do what you want to do because
millions of .NET developers don't use a Boolean object ; )
 
H

Henri

In java for instance there's a way to use booleans as objects and not as
value types.
I would like to do the same in VB.NET so that I can check if the boolean has
been explicitely defined (is not Nothing).
But Boolean is a Structure in VB.NET (defined to False by default) and not
an Object so I'm afraid there's no way to do what I want without any
workaround.
Can you confirm that there's no object version of Boolean in VB.NET?
Thanks

Henri
 
S

Scott M.

Couldn't you just make a class that contains a public property and has a
private Boolean field? The value of the private Boolean field determines
the value of the public property? If the private field value hasn't been
set, then the public property value stays at its default (which you set to
Nothing in the class constructor).
 
A

_AnonCoward

:
: In java for instance there's a way to use booleans as objects and not
: as value types.
: I would like to do the same in VB.NET so that I can check if the
: boolean has been explicitely defined (is not Nothing).
: But Boolean is a Structure in VB.NET (defined to False by default) and
: not an Object so I'm afraid there's no way to do what I want without
: any workaround.
: Can you confirm that there's no object version of Boolean in VB.NET?
: Thanks
:
: Henri


Perhaps you could 'box' the boolean type along these lines:

Dim BooleanObj As Object = New Boolean


For Example:

---------------------------------------------------
Option Strict
Imports System

Public Class [class]
Public Shared Sub Main

'Outputs "True"
Dim BooleanObj As Object
Console.Writeline(BooleanObj Is Nothing)

'Outputs "False"
BooleanObj = New Boolean
Console.Writeline(BooleanObj Is Nothing)

'Outputs "False"
Console.WriteLine(BooleanObj)

'Outputs "True"
BooleanObj = True
Console.WriteLine(BooleanObj)

End SUb
End Class
---------------------------------------------------


Or if you prefer, you can write your own class:


---------------------------------------------------
Public Class BooleanObj
Private b As Boolean

Public Sub New
End Sub

Public Sub New(Value As Boolean)
b = Value
End Sub

Public Property Value() As Boolean
Get
Return b
End Get
Set
b = value
End Set
End Property

Public Overrides Function ToString() As String
Return b.toString
End Function

Public Overrides Overloads Function Equals(obj As Object) As Boolean

If obj Is Nothing Then
Return False
End If

If Not Me.GetType() Is obj.GetType() Then
Return False
End If

Dim o As BooleanObj = CType(obj, BooleanObj)
If b <> o.b Then
Return False
End IF

Return True

End Function

End Class
 
M

m.posseth

well i guess this is a easy way


Dim objBool As Object = Nothing



objBool = True ' or objBool = false

If IsNothing(objBool) Then

MsgBox("i am not set")

Else

MsgBox(CType(objBool, Boolean))

End If

however there are so manny roads to Rome :) some may go pass Paris or
Madrid



regards

Michel Posseth [MCP]
 
H

Herfried K. Wagner [MVP]

Henri said:
In java for instance there's a way to use booleans as objects and not as
value types.
I would like to do the same in VB.NET so that I can check if the boolean
has
been explicitely defined (is not Nothing).
But Boolean is a Structure in VB.NET (defined to False by default) and not
an Object so I'm afraid there's no way to do what I want without any
workaround.
Can you confirm that there's no object version of Boolean in VB.NET?

You may want to use a class that wraps a boolean value instead of the
standard 'Boolean' type:

\\\
Public Class NullableBoolean
Public Sub New()
'
End Sub

Public Sub New(ByVal Value As Boolean)
Me.Value = Value
End Sub

Public Value As Boolean
End Class
///

- or -

\\\
Public Structure NullableBoolean
Public Value As Boolean
Public IsNull As Boolean
End Structure
///

The .NET Framework contains an 'SqlBoolean' structure which is nullable.

In VB 2005 you can use generics ('Nullable') to create a nullable boolean
type ('Nullable(Of Boolean)'.
 
A

Alex

Herfried K. Wagner said:
In VB 2005 you can use generics ('Nullable') to create a nullable boolean
type ('Nullable(Of Boolean)'.

I tried that with VB 2005 but I didn't get the expected results. What am I
missing?

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim x As Nullable(Of Boolean)

MsgBox(String.Format("IsNothing( x ) = {0:G}", IsNothing(x)))

End Sub

IsNothing(x) is returning False, not the True that I expected. I also tried
declaring x with:

Dim x As Nullable(Of Boolean) = Nothing

but I got the same result (False).
 
H

Herfried K. Wagner [MVP]

Alex said:
I tried that with VB 2005 but I didn't get the expected results. What am
I
missing?

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim x As Nullable(Of Boolean)

MsgBox(String.Format("IsNothing( x ) = {0:G}", IsNothing(x)))

End Sub

IsNothing(x) is returning False, not the True that I expected. I also
tried
declaring x with:

Dim x As Nullable(Of Boolean) = Nothing

but I got the same result (False).

You'll have to check the object's 'HasValue' property in order to determine
whether or not a value has been assigned. VB 2005 won't have built-in
support for nullable types.
 
A

Alex

Herfried K. Wagner said:
You'll have to check the object's 'HasValue' property in order to determine
whether or not a value has been assigned.
Thanks.

VB 2005 won't have built-in support for nullable types.

I am confused by that statement. How's that different than with C#? What kind
of support will C# get that VB won't?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top