|
I was wondering, how do computers store a null value in their RAM?
|
|
|
|
|
And what does this have to do with Null values?
|
|
|
|
|
If you are going to say that 'null'is zero then it is not.
Garbage? of course not.
So what is a 'null' value?
|
|
|
|
|
A Null value is basically, nothing. It's not even 0, it's just a blank value.
|
|
|
|
|
Ok. How do you represent a 'blank value' in C++
|
|
|
|
|
its Dependant on the language/framework or in the case of C++ the compile that was used. usually in memory it will be represented as '\0', but this can change per architecture.
|
|
|
|
|
Now can be have type specific null values?
I mean an integer null, a double null etc.
|
|
|
|
|
Sometimes null's are convenient and comprable (a useful feature), such as in C/C++, with a fine value which can be evaluated (such as for pointers). null == null (== 0)
In this case, it depends upon the context, but it is usually used. There is an assumption that the compiler initializes non-pointer types (such as int) to 0, or its equivalents.
On the other hand, SQL nulls are undefined. They can be used, but the only real operation is to test for them. In SQL, null != null.
In this case, I may allow nulls to make data entry simpler, but just as often, I'll create a default value for a field so I can test for it mixed with non-null'd data. There was an application I created that used zero as a possible true value, and my 'null' was -99.99 .
So, upon general consideration, I tend to use evaluatable null's when available. Often, such as pointer values, this is the only sensible way to go if you wish to compare those assigned data space with those that have none.
Definitely a lot of personal taste involved in this.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
|
|
|
|
|
When I've seen this survey my first thought was that you're thinking on nullable types in .net 2.0, so I've voted No! I don't like nullable types and I don't use them! But after reading comments, I see someone is talking about null value for reference types too. In that case, of course I use null values! How can you live without them
|
|
|
|
|
And what do you think is wrong with nullable types?
Let's say for example an age of a person, so you got a Person class with an int Age property.
The question now is, would you use -1 to indicate that no age is filled in? You can't use 0 as it is a valid age. You could even use uint since an age can't be negative.
I find nullable types a perfect solution for a struct type that has no valid state yet.
if (person.Age != null) is much much better then if (person.Age != -1)
|
|
|
|
|
I would agree with you, if int and int& weren't different types. I hate to convert all the time. To change all variables and use all nullable types isn't solution for big projects.
Also, I have lot of negative experience with null values in database, I don't want them in my code too.
About:
if (person.Age != -1)
I would (probably) define constant Undefined = -1 and check if (person.Age != Undefined) and if you want to use uint then Undefined would have value of uint.MaxValue.
I think this is good solution without introducing new data type.
|
|
|
|
|
Nullable types in .NET 2.0 are moderately useful in your general everyday code. However, its when you start working with SQL queries, inserting, updating, and selecting data, that the Nullable type shows its true value. Without nullable types, you have to explicitly check if a value selected is equal to DBNull. This can make for some tedious and pointless coding when you work with database data in .NET.
With nullable types, however, the need to check for DBNull is diminished. You just use a nullable type, and if its null, then there ya go. Nullable types have other uses, but this was the primary one, I believe.
|
|
|
|
|
But I must admit that I've really missed nullable DateTime variables in .net 1.1
|
|
|
|
|
Is some one suggesting there's something wrong with null?
I thought it was an essencial part of c++ and java (never used c#)
|
|
|
|
|
And your rights with Windows!
Brad
Australian
I assume Microsoft would not use doors, because using Windows is faster.
|
|
|
|
|
Using a NULL, DBNull are also 'special cases', but is not part of a contiguous string of numbers. If you use -1 as a magic number, then what if you really wanted that field to contain -1 and not its special meaning? Why not, then, -3589282? or 9322773943? or if a string, "I love CodeProject"?
It's not always the best design to encode function into a value. You can do it and it can work, but you may need to deal with its limitations later.
---
Shawn Poulson
spoulson@explodingcoder.com
|
|
|
|
|
Having had to use 9999 as a "magic number" in legacy databases I agree that they can cause problems later in life.
djj
|
|
|
|
|
they're useful if you know for certain that the value will never need to be that.
say you make a value positive - if it works, it becomes the changed value and if it doesn't, it becomes -1
since Abs(anything) != -1 you know that -1 works in all conditions and is guaranteed only to happen during an error.
|
|
|
|
|
In cases where I don't need nullable types, I always use one of the value type static properties such as MinValue or MaxValue to represent the unset state of a variable. I like this because it takes the choice further out of band than a -1, and is generally consistent across many data types regardless of size. It also stands out a lot more than -1.
Dale Thompson
|
|
|
|
|
In cases where I don't need nullable types, I always use one of the value type static properties such as MinValue or MaxValue to represent the unset state of a variable. I like this because it takes the choice further out of band than a -1, and is generally consistent across many data types regardless of size. It also stands out a lot more than -1.
Dale Thompson
|
|
|
|
|
All data types support null. Then null is actually more consistent than anything else. =P Plus, MinValue and MaxValue differ across types. (compare Int16 and Int64). -1 can be represented by all signed integral types.
ROFLOLMFAO
|
|
|
|
|
>>All data types support null
Are you sure in that? What about int, float?
|
|
|
|
|
Kamarey wrote: >>All data types support null
Are you sure in that?
Positivly.
What about int, float?
int i = 0;
float f = 0.0; If you need some "Out of band" signaling for "not valid", I personally would use a separate bool for valid or not.
Thats what C# is doing behind the curtains with its nullable types anyway.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation."
-- Caius Petronius, Roman Consul, 66 A.D.
|
|
|
|
|
Exactly what the previous guy said. int?, bool?, decimal?, etc.
ROFLOLMFAO
|
|
|
|
|
Gotta love it when using C#. Although I have to say that it sometimes is unclear what the compiler will put in place.
WM.
What about weapons of mass-construction?
"You can always try to smash it with a wrench to fix that. It might actually work" - WillemM
|
|
|
|