P
Pavils Jurjans
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
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