469,602 Members | 1,962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Special value for "undefined" value

Hello,

I am building custom hashtable class, and thinking about value retrieval
issues. The thing is, that sometimes the hashtable value may contain value
null. If someone is reading this value like this
value = myHashtable["myKey"];

then, is the value of "myKey" key is null, then the result is the same, as
if there would be no such value in the hashtable.

If course, I could do check, using "Contains" method to get boolean value
describing wether that value is in the hashtable, and if it it there, read
it and use it.

if (myHashtable.Contains("myKey")
{
value = myHashtable["myKey"];
// use value
} else {
// do stuff if value doesn't exist
}

But, with this I don't like the inefficient process, that since this is
custom Hashtable, I have to provide the Contains method myself, and it is
actually very close to the indexer method that actually reads the value
instead of existence status. Thus, if I first call "Contains" and then read
the value, I effectively call the same code twice, which is inneffective,
especially if the value/existence retrieval process is processing-heavy (ie,
reading from file, database, or web service). Therefore, I was looking for
some sort of "undefined" value type, that I could return in case if the key
is not found in the hastable. I've been programming for long years in Java
and JavaScript, and been using to this special variable type. If such type
would exist, my code would be more effective:

value = myHashtable["myKey"];
if (value == undefined)
{
// do stuff if value doesn't exist
} else {
// use value
}

Current temporary solution I am using, is that I have created a dummy class
"Undefined", and when it is needed to return the undefined value, I return
an instance of this dummy class. The user side, however, can check wether
the returned value is instance of the dummy class:

value = myHashtable["myKey"];
if (value is Undefined)
{
// do stuff if value doesn't exist
} else {
// use value
}

But, I somewhet feel uneasy that I have to create a new type, and a very
stupid class, indeed. I thot maybe there is some type handy already, that I
could use, maybe there is some an industry-accepted approach how to handle
this question.

Thanks,

Pavils
Nov 16 '05 #1
1 2885
Hi,

"System.Data" namespace are using the "DBNull.Value" to
get "NULL" equivalent.

If You want to use this approach then define in your "Undefined"
class static readonly property "Value" that will contains
single instance of your class.

Then you'll check it:

if (value==Undefined.Value) {
...
}

Regards

Marcin
Hello,

I am building custom hashtable class, and thinking about value retrieval
issues. The thing is, that sometimes the hashtable value may contain value
null. If someone is reading this value like this
value = myHashtable["myKey"];

then, is the value of "myKey" key is null, then the result is the same, as
if there would be no such value in the hashtable.

If course, I could do check, using "Contains" method to get boolean value
describing wether that value is in the hashtable, and if it it there, read
it and use it.

if (myHashtable.Contains("myKey")
{
value = myHashtable["myKey"];
// use value
} else {
// do stuff if value doesn't exist
}

But, with this I don't like the inefficient process, that since this is
custom Hashtable, I have to provide the Contains method myself, and it is
actually very close to the indexer method that actually reads the value
instead of existence status. Thus, if I first call "Contains" and then read
the value, I effectively call the same code twice, which is inneffective,
especially if the value/existence retrieval process is processing-heavy (ie,
reading from file, database, or web service). Therefore, I was looking for
some sort of "undefined" value type, that I could return in case if the key
is not found in the hastable. I've been programming for long years in Java
and JavaScript, and been using to this special variable type. If such type
would exist, my code would be more effective:

value = myHashtable["myKey"];
if (value == undefined)
{
// do stuff if value doesn't exist
} else {
// use value
}

Current temporary solution I am using, is that I have created a dummy class
"Undefined", and when it is needed to return the undefined value, I return
an instance of this dummy class. The user side, however, can check wether
the returned value is instance of the dummy class:

value = myHashtable["myKey"];
if (value is Undefined)
{
// do stuff if value doesn't exist
} else {
// use value
}

But, I somewhet feel uneasy that I have to create a new type, and a very
stupid class, indeed. I thot maybe there is some type handy already, that I
could use, maybe there is some an industry-accepted approach how to handle
this question.

Thanks,

Pavils

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Denis Perelyubskiy | last post: by
13 posts views Thread by Don Vaillancourt | last post: by
3 posts views Thread by Mark Sullivan | last post: by
49 posts views Thread by matty | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.