Home All Groups Group Topic Archive Search About

Assigning Nullables to each other

Author
16 Jun 2006 2:18 PM
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

Author
16 Jun 2006 4:12 PM
Mattias Sjögren
>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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
16 Jun 2006 5:40 PM
BobRoyAce
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
Author
16 Jun 2006 6:56 PM
Mattias Sjögren
>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 Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
16 Jun 2006 9:31 PM
BobRoyAce
Mattias Sjögren wrote:
> >what would the Function declaration look like?
> >
> >
> >Function GetNullableIntFromIntField(ByVal oSomething As ???) As ???
>
>
> 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."
Author
17 Jun 2006 10:11 AM
Mattias Sjögren
>> 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"

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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
18 Jun 2006 4:50 AM
BobRoyAce
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...