Nothing Does Not Work As Documented

B

Bob Day

VS 2003

The documentation says " Nothing keyword represents the default value of any
data type" this is simply not true and causing a lot of problems.

1) Consider an SQL table of 3 columns:

Column1 bit no nulls
Column2 string no nulls
Column3 DateTime no nulls

2) then, set each column in a DataSet, DataRowto nothing
Column1 = nothing ' should be 0 false
Column2 = nothing ' should be string.empty
Column3 = nothing ' should be 01/01/0001
If you look at the values in DataRow, Column 1,2 & 3 they do not reflect the
defaults, although they all have some value in them (particularly true with
DateTime)

3) Now, using a DataAdapter for the table add the DataRow to the DataSource.

It fails everytime with a message like: "Cannot write null to non-nullable
Column1".

4) If you manually set the DataRow columns (1,2,3) to their default values,
it works every time.

What does that mean? Nothing does not work. Very frustrating.

Please advise.

Bob Day
 
W

William Ryan

Bob, setting the column to nothing does just that, it sets it to the default
value of the object not to the default value that you assigned the object.

Column1 = nothing

Then try referencing Column1 will give you a null reference exception. All
of those columns are now null, so if you try adding a row with them in it,
you'll get that exception.

The confusion comes from the word Default. Any given column can havea
default value that you assign. The Default value for a Column is nothing,
meaning that you haven't defined it yet.

Try setting Column2 to string.Empty for instance, Column1 to 0 and column3
to 01/01/0001 and it will work. Also, since you set them to nothing, they
very well may be garbage collected and cease to exist.

HTH,

Bill
 
B

Bob Day

I really don't understand the distinction you are tyring to make. Below is
the documentation.
It says that dim x as boolean = nothing, then x should have a value of
False, the same as if you did dim x as boolean = false.

In other words, these two sould produce the same value:
1) dim x as boolean = nothing
2) dim x as boolean = false

1 fails, 2 writes to a datasource just fine.

Default, as defined below, is the default of the data type, not <NULL>.

Please help me understand.

Bob

Documentation for help:
The Nothing keyword represents the default value of any data type. Assigning
Nothing to a variable sets it to the default value for its declared type. If
that type contains variable members, they are all set to their default
values. The following example illustrates this:

Public Structure MyStruct
Public Name As String
Public Number As Short
End Structure
Dim S As MyStruct, I As Integer, B As Boolean
S = Nothing ' Sets S.Name to Nothing, S.Number to 0.
I = Nothing ' Sets I to 0.
B = Nothing ' Sets B to False.If the variable is of a reference type -
that is, an object variable - Nothing means the variable is not associated
with any object. For example:

Dim MyObject As Object
MyObject = Nothing ' No object currently referred to.When you assign
Nothing to an object variable, it no longer refers to any object instance.
If the variable had previously referred to an instance, setting it to
Nothing does not terminate the instance itself. The instance is terminated,
and the memory and system resources associated with it are released, only
after the garbage collector detects there are no active references
remaining.
 
A

alien2_51

You cannot test a Boolean, Integer DateTme or any other "Value" type for
Nothing... You must use a reference type if you want to check for Nothing...
A Boolean datatypes initial value, hence the term "Value Type" is False and
Integer is 0 a DataTime is 01/01/0001...
Try this
Dim bol As Boolean

If bol Is Nothing Then

End If

Dim str As String

If str Is Nothing Then

End If

The second test does not produce an error because String is a "Reference
Type"

Does that help....?
 
W

William Ryan

Bob:

Look at your the documentation you posted:

' No object currently referred to.When you assign
Nothing to an object variable, it no longer refers to any object instance.>>

Ok, so you have an instance of a column and set it to nothing ..."it no
longer refers to any object instance" If it doesn't refer to any instance
object, then how could you determine its type?

Let me state it more pragmatically....If you want to use the object,
determine it's type, set it's value etc...setting it to nothing will cause
problems.


Now I ask you... Are reference types, ie DataColumns and Value Types
Structs, treated the same when set to nothing? Therein lyes the answer to
your question.
 
W

William Ryan

Bob: The whole distinction, is this...are DataColumns Reference types or
Value Types? What type is a Struct that you make mention of?
 
W

William Ryan

Amen:

Since it's a reference type, it needs to be treated as one. Structs, Enums
etc are value types. DataColumns are reference types.
 

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

Top