Inconsistent Nothing

B

Bob Day

VS 2003, vb.net, sql native (MSDE)...

I have railed against the inconsistency of the Nothing key word. The
documentation says is will assign a default value for any datatype...well,
not exactly. Here are 2 more examples:

Example 1) dim x as string = nothing, the value of x is nothing (instead of
string.empty). When you add a row to SQL, the column with the value of X
will fail in a table that does not allow nulls because Nothing is translated
to DBNull. So, the default value of a string is DBNull? That is not very
intuitive.

Example 2) dim y as Boolean = nothing, the value of x is false (which makes
sense). This writes to an SQL table fine as 0 (false)

However, consider a Boolean column datatype, DataColumn.defaultvalue =
nothing. When you add a row to SQL, the Boolean column with the value of
..defaultvalue = nothing will fail in a table that does not allow nulls
because Nothing is translated to DBNull (instead of false). So, the
..defaultvalue of a Boolean is DBNull? That is not very intuitive.

The above are replicatable every time.

These inconsistencies with the documentation about Nothing are frustrating
and time consuming. Someone should spend the time to make Nothing
consistent in a future release - it would be appreciated.

Bob Day
 
C

CJ Taylor

I don't know if I would say its not "intuitive" in fact, I think it very
much is.

There is a major difference between an empty string and a null value. As
well as understanding the difference between Nothing and and empty value.
Nothing says it doesn't exist, Empty says it exists but with no value. See
the difference?

Also, there are many more applications that don't use DB's, where an Empty
is different from a Null. To predict that within the language would be
impossible. So the option is given to the programmer.

Hope it helps. I know it doesn't make sense, but its really understanding
what an object is vs. a value.

-CJ
 
C

Cor

Bob,

Did you try this?
If Nothing = String.Empty Then
MessageBox.Show("empty")
End If

I would not say it is all direct clear with "Nothing" but for me the
inconsequenties in the SQL syntax are worser.

Cor
 
C

CJ Taylor

Cor said:
Bob,

Did you try this?
If Nothing = String.Empty Then
MessageBox.Show("empty")
End If

I would not say it is all direct clear with "Nothing" but for me the
inconsequenties in the SQL syntax are worser.

worser?????????

is that like having the proper strategery?
 
C

Cor

Hi CJ,
That's better.
That's worse.
It shows that English is maybe not the best language to use with
programming.
It seems to be a not consistent language.
;-)
Cor
 
C

Codemonkey

Nothing is the default (uninitialized) value for an *object* type. The
default (uninitialized) value for a *value* type is up to the language.

A string is an object type, so you can set it to nothing. String.Empty is
not nothing (in some implementations it contains 1 character - the null
character or Chr(0) to signify the end of the string). In most databases, an
empty string is also treated differently than Null.

Boolean, Integer, Floating Point variables etc. are value types and so
cannot be set to nothing. Instead they are set to the default value for that
type (0, False etc.)

If you need empty strings to be placed in your table, declare them as

Dim szMyString as String = ""

HTH,

Trev.
 
H

Herfried K. Wagner [MVP]

CJ Taylor said:
There is a major difference between an empty string and a null value. As

In some DBMS strings of zero length (empty strings) are treated as
NULL. That's IMO very unintuitive, because there is a semantical
difference beteen a zero-length string an a null value. NULL means
"unknown" to me, a zero length string means that the information is "".

;-)
 
H

Herfried K. Wagner [MVP]

Cor said:
Did you try this?
If Nothing = String.Empty Then
MessageBox.Show("empty")
End If

'String.Empty' is a zero-length string, "". The default value of a
string is "" too.
 
H

Herfried K. Wagner [MVP]

Cor said:
That's better.
That's worse.
It shows that English is maybe not the best language to use with
programming.
It seems to be a not consistent language.

Latin would be better...

;-)
 
C

Cor

Hi Herfried,
You maybe are kidding and I don't know it at all, but I sometimes think that
is true.
Is the modern Latin consistent, it has been for years the language of the
scienctific?
Cor
 
C

Cor

Stephany,
Are you defiantly sure of that, do you think that the first Romans did speak
the same language as the now official language from the country Vatican.
I really don't know it. (I have putted the word "modern" in to give an
accent about the official used scientific Latin, but maybe I had better done
to write it in that way.)
Cor
 
S

Stephany Young

The Latin language that is spoken in the Vatican and by scholars in various
universities is the same Latin as was spoken by the Romans.

In recent years new 'latin' words have been coined to represent modern words
and phrases like telephonium albo televisifico coniunctum for video
telephone, tempus maximae frequentiae for rush hour, publicae securitatis
custos internationalis for Interpol and officium foederatum vestigatorium
for FBI. This, by no means amounts to 'modernising' the language.
 
H

Herfried K. Wagner [MVP]

Stephany Young said:
The Latin language that is spoken in the Vatican and by scholars
in various universities is the same Latin as was spoken by
the Romans.

I think the pronounciation may be slightly different...

;-)))
 
S

Stephany Young

One of the beauties of Latin is that, because of the 'strictness' of the
language there is only one way to pronounce any given word. With Latin there
are no grey areas and there is no room for variation.
 
C

Codemonkey

'String.Empty' is a zero-length string, "". The default value of a
string is "" too.

I don't agree that the default value of a string in VB.net is "". A string
is an object, so it's default value is Nothing (Unlike VB6).

Take the following example:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim szTest as String

Debug.Write(szTest) ' Writes nothing to the output
Debug.Write(Nothing) ' Writes nothing to the output
Debug.Write(szTest.Length) ' Raises NullReferenceException
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The third Debug.Write will raise a NullReferenceException as the default
value of szTest is Nothing. If you look at szTest in the watch window, it's
value is "Nothing"

The reason why no error is raised on the first two calls to Debug.Write is
because Debug.Write can take Nothing as a parameter.

If you declared your string as:

Dim szTest as String = ""

Then the default value of that string would be "" as opposed to Nothing.


Sorry for being a stick in the mud ;-)
 
C

Cor

Codemonkey,
I take another approach (I come on this, because it was a question in this
newsgroup yesterday). A string is an array and as we are used to now, it is
an array of characters (in the past bytes, but now we have Unicode
characters).

How many characters are there in a string "" ?

Cor
 
M

Mark Pearce

Hi Bob,
<<

The default value of a string variable in VB .NET is Nothing, because String
is a reference type. What's happening is that your data access provider (the
component that controls the database connection) is translating Nothing to
Null, which means "unknown" in any proper RDBMS. This has nothing to do with
VB or .NET - it's just the way the data access component that you're using
works.

The default value of a boolean variable in VB .NET is False, because Boolean
is a value type and a value type can't contain Nothing. However, a
DataColumn of type boolean *must* be able to have a default value of
Nothing, because the associated database column might allow Null as a valid
value.

Think about it - if a DataColumn of type boolean couldn't have a value of
Nothing, how would you be able to set the associated database column to
Null?

HTH,

Mark
--
Author of "Comprehensive VB .NET Debugging"
http://www.apress.com/book/bookDisplay.html?bID=128


VS 2003, vb.net, sql native (MSDE)...

I have railed against the inconsistency of the Nothing key word. The
documentation says is will assign a default value for any datatype...well,
not exactly. Here are 2 more examples:

Example 1) dim x as string = nothing, the value of x is nothing (instead of
string.empty). When you add a row to SQL, the column with the value of X
will fail in a table that does not allow nulls because Nothing is translated
to DBNull. So, the default value of a string is DBNull? That is not very
intuitive.

Example 2) dim y as Boolean = nothing, the value of x is false (which makes
sense). This writes to an SQL table fine as 0 (false)

However, consider a Boolean column datatype, DataColumn.defaultvalue =
nothing. When you add a row to SQL, the Boolean column with the value of
..defaultvalue = nothing will fail in a table that does not allow nulls
because Nothing is translated to DBNull (instead of false). So, the
..defaultvalue of a Boolean is DBNull? That is not very intuitive.

The above are replicatable every time.

These inconsistencies with the documentation about Nothing are frustrating
and time consuming. Someone should spend the time to make Nothing
consistent in a future release - it would be appreciated.

Bob Day
 
H

Herfried K. Wagner [MVP]

Stephany Young said:
One of the beauties of Latin is that, because of the
'strictness' of the language

An 'Option Strict On' language...

;-)
there is only one way to pronounce any given word. With
Latin there are no grey areas and there is no room for
variation.

ACK. Nevertheless, some letters can be pronounced differently, for example
"a" is pronounced in a different way by German speakers than by English
speakers. When I "spoke" Latin at school, I used the German pronounciation.
 

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