By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 927 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Henri" <hm********@hotmail.com> wrote in
news:un**************@TK2MSFTNGP12.phx.gbl:

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 ; )

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2

P: n/a
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).


"Henri" <hm********@hotmail.com> wrote in message
news:un****************@TK2MSFTNGP12.phx.gbl...

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

Nov 21 '05 #3

P: n/a

"Henri" <hm********@hotmail.com> wrote in message
news:un****************@TK2MSFTNGP12.phx.gbl...
:
: 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
---------------------------------------------------
Ralf
Nov 21 '05 #4

P: n/a
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]

"Henri" <hm********@hotmail.com> wrote in message
news:un****************@TK2MSFTNGP12.phx.gbl...

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

Nov 21 '05 #5

P: n/a
"Henri" <hm********@hotmail.com> schrieb:
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)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eV**************@TK2MSFTNGP09.phx.gbl...
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).



Nov 21 '05 #7

P: n/a
"Alex" <pl******@no.messages> schrieb:
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).


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O%***************@TK2MSFTNGP15.phx.gbl...
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?

Nov 21 '05 #9

P: n/a
"Alex" <pl******@no.messages> schrieb:
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.


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


C# will support some additional type keywords and operators for dealing with
nullable types:

<URL:http://msdn2.microsoft.com/library/2cf62fcy(en-us,vs.80).aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10

P: n/a

Thank you so much for your help to all of you!!!
:-)

Henri
"Henri" <hm********@hotmail.com> a écrit dans le message de
news:un****************@TK2MSFTNGP12.phx.gbl...


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


Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.