I am new to Nullable types.
Let's say that I have a variable as follows:
Dim iID As Nullable(Of Integer)
Now, let's say that I have code that either needs to assign an integer
value to iID or have it be NULL. For simplicity, let's use...
If (True) Then
iID = 12
Else
iID = Nothing
End If
At this point, if iID = Nothing was executed, iID = 0 and iID.HasValue
returns True! What do I assign to iID such that iID.HasValue will
return False?
> I am new to Nullable types.
>
[quoted text clipped - 15 lines]
> iID.HasValue returns True! What do I assign to iID such that
> iID.HasValue will return False?
Can't reproduce it. After assigning Nothing, iId.Hasvalue returns false.
Armin
BobRoyAce - 20 Mar 2008 01:50 GMT
Oops...I figured out what the problem was. I was assigning the value
via a function that was returning an Integer. In certain
circumstances, I was returning Nothing, which ultimately returned ZERO
(0). I just had to change the function so that it returned a
Nullable(Of Integer). Silly me...
Bob,
I am always curious why people will use what you are doing, a nullable
integer is in fact a worse thing from the past.
What is your reason?
(Be aware that a integer that is null, has nothing to do with the value type
DBNull.Value which can be inside an object)
Cor
>I am new to Nullable types.
>
[quoted text clipped - 14 lines]
> returns True! What do I assign to iID such that iID.HasValue will
> return False?
Patrice - 20 Mar 2008 19:31 GMT
This is for example used in LINQ to SQL to handle nullable columns. Yes I
know this is not the same thing but just a "representation", I believe we
already had this discussion without understanding much each other....
Likely more usefull for date columns as they can usually hold a date for a
future event in which case those columns are made nullable than for any
other data type...
--
Patrice
> Bob,
>
[quoted text clipped - 7 lines]
>
> Cor
BobRoyAce - 20 Mar 2008 20:50 GMT
> Bob,
>
> I am always curious why people will use what you are doing, a nullable
> integer is in fact a worse thing from the past.
>
> What is your reason?
I use Nullable(Of Integer) for variables that will store foreign key
references to other tables. So, let's say that I have a class called
LoanApplication, with a property called fkDeclinedByUserID. This
property must either be NULL, or have an integer value corresponding
to the primary key field of a User record. In my code that does the DB
update or insert, I check to see if fkDeclinedByUserID.HasValue is
True. If it is, I pass in the integer to the stored procedure
parameter; otherwise, I pass in System.Convert.DBNull.
Etxe - 20 Mar 2008 23:32 GMT
I use a table whith a field that is relation whith another table
I use a from with textboxs binding to a dataset
When I want to clear the value of this field it receive the "" value and it
can be save in the bd because the relational table not have "" record,
I need to give null value before update the record, how can i do?
Thanks
Sorry my English
>> Bob,
>>
[quoted text clipped - 11 lines]
> True. If it is, I pass in the integer to the stored procedure
> parameter; otherwise, I pass in System.Convert.DBNull.