Nullable object types

N

Nemisis

Hi everyone, i have been stressing over this for the past hour or so,
and just cannot figure it out, sorry if i am being stupid, but it is a
friday! lol

I have the following

Private _ID as nullable (Of integer)

Public Property ID()
Get
Return _ID
End Get
Set(ByVal value)
_ID = value
End Set
End Property

If i write
Me.ID = 1234

then the value in the debugger seems to update, great.

But how do i get it back to null again? If i set ID = DBNull.Value, i
get a build error. If i set ID = Nothing, then in the debugger, it
says "Nullable object must have a value".

If i am missing the point and being stupid, i do apoligise, i just
really need to get this done.
 
P

Patrice

Try :
Me.ID = Nothing

MsgBox(Me.ID.HasValue)



Use Option Strict so that youi see you should make the propery IsNullable(Of
Integer) instead of just Object...
 
N

Nemisis

Patrice said:
Try :
Me.ID = Nothing

MsgBox(Me.ID.HasValue)



Use Option Strict so that youi see you should make the propery IsNullable(Of
Integer) instead of just Object...
I have tried setting the property to nothing and that still doesnt
work. ???
 
P

Patrice

As I said, you also have to correctly type your property :
Private _ID As Nullable(Of Integer)

Public Property ID() As Nullable(Of Integer)

Get

Return _ID

End Get

Set(ByVal value As Nullable(Of Integer))

_ID = value

End Set

End Property

You can then set the property to nothing :
Me.ID = 1234

Me.ID = Nothing

MsgBox(Me.ID.HasValue)


--
Patrice

"Nemisis" <[email protected]> a écrit dans le message de
(e-mail address removed)...
Try :
Me.ID = Nothing

MsgBox(Me.ID.HasValue)



Use Option Strict so that youi see you should make the propery
IsNullable(Of
Integer) instead of just Object...
I have tried setting the property to nothing and that still doesnt
work. ???
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top