Assigning Nullables to each other

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
B

BobRoyAce

In my property declaration, shown below, is it
correct to assign _DeptID = Value like I would have if it was just a
plain ole Integer, or is this like setting two objects equal to each
other where the end result is that the one on the left now points to
the same memory address as the one on the right? Should I instead be
assigning _DeptID.Value = Value.Value or _DeptID.Value =
CInt(Value.Value)?

Public Property DeptID() As Nullable(Of Integer)
Get
Return _DeptID
End Get
Set(ByVal Value As Nullable(Of Integer))
If Not Value.Equals(_DeptID) Then
_DeptID = Value
_HasChanged = True
End If
End Set
End Property ' DeptID
 
In my property declaration, shown below, is it
correct to assign _DeptID = Value like I would have if it was just a
plain ole Integer, or is this like setting two objects equal to each
other where the end result is that the one on the left now points to
the same memory address as the one on the right?


Nullable(Of T) is a structure, so you'll store a copy of the value.


Mattias
 
Thanks Mattias...sounds like it's cool the way I'm doing it then.
Another question, if you don't mind...

If I wanted to create a function, called let's say
GetNullableIntFromIntField, which took as its input an integer field
coming in from a DB
(e.g. rdr("DeptID")) and gave back either Nothing or the integer value,

what would the Function declaration look like?


Function GetNullableIntFromIntField(ByVal oSomething As ???) As ???
If IsDBNull(oSomething) Then
Return Nothing
Else
Return CInt(oSomething)
End If
End Function
 
what would the Function declaration look like?
Function GetNullableIntFromIntField(ByVal oSomething As ???) As ???


Function GetNullableIntFromIntField(ByVal oSomething As Object) As
Nullable(Of Integer)

or if you want to generalize it a bit

Function GetNullableFromField(Of T As {Structure})(ByVal oSomething As
Object) As Nullable(Of T)
If IsDBNull(oSomething) Then
Return Nothing
Else
Return CType(oSomething, T)
End If
End Function


Mattias
 
Mattias said:
Function GetNullableIntFromIntField(ByVal oSomething As Object) As
Nullable(Of Integer)

This gives me an error message, in the editor environment stating
"Nullable is a type and cannot be used as an expression"
or if you want to generalize it a bit

Function GetNullableFromField(Of T As {Structure})(ByVal oSomething As
Object) As Nullable(Of T)
If IsDBNull(oSomething) Then
Return Nothing
Else
Return CType(oSomething, T)
End If
End Function

I like the idea of a general function, since I have to deal with Dates
and Booleans as well. However, I get error messages, when trying to
Build, related to this one...

Error 9 'Object' is a class type and cannot be used as an expression.

In the editor environment it states, when I hover over the function
name, "Function without an 'As' clause; return type of object assumed."
 
Function GetNullableIntFromIntField(ByVal oSomething As Object) As
This gives me an error message, in the editor environment stating
"Nullable is a type and cannot be used as an expression"

Put it all on the same line (or use the _ line continuation
character). It's just broken up by my newsreader. It should be (if we
shorten the names a bit)

Function X(ByVal y As Object) As Nullable(Of Integer)


Mattias
 
Yup...that did it...was obviously up way too late copying and pasting
that code in...not paying attention to an obvious thing like that!
Thanks...
 
Back
Top