473,573 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Co ntains("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 3099
Hi,

"System.Dat a" namespace are using the "DBNull.Val ue" 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==Undefin ed.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.Co ntains("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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1852
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET 2003, although I'm not sure what is actually causing the problem. I can't summarize the issue, so please read on to find out more. I've created a...
2
3518
by: Denis Perelyubskiy | last post by:
Hello, I have a form with an input field, a textarea, having a value of "" Also, I have some javascript code, which may be reduced to the following: function validate(mycode) { alert(mycode.value == undefined); }
13
3070
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null...
3
29719
by: Mark Sullivan | last post by:
When I trace through a csharp program I came to a situation where a certain values has an "undefined value" as shown in the debugger DbgClr. I want to check this but the following statements did not recognize this "non"-value if (type.Particle != Undefined.Value) { .... or if (type.Particle != null) { .....
49
14452
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
1
5894
by: OtisUsenet | last post by:
Hi, I have a bookmarklet that works perfectly in Firefox, IE, Konqueror, and Opera, but in Safari 2.0.3 (417.9.2) it doesn't work. I enabled debugging and I can see "TypeError - Undefined value" reported in the Javascript console, but I'm not sure where the error is. The bookmarklet in question is here: javascript:if...
2
12620
by: gmccammon | last post by:
I am getting an error that says that I can't call method "prepare" on an undefined value! Here is what I have as code.... I don't understand what is not defined. # opens connection to Access database titled KTC.mdb # no passwords on our database $dbh = DBI->connect('dbi:ODBC:KTC'); $connect = DBI->connect('dbi:ODBC:KTC');
11
14506
by: gautamga | last post by:
Hi All i have created the script which calls method from other script lib and while executing i get and error Can't call method "prepare" on an undefined value at /home/patriat/sourcescr/input/lib/patriat_lib.pl line 226. # conect to database $dbh = DBI->connect( $ENV{ORACLE_SID}, $ENV{ORAUSER}, $ENV{ORAPASSWD}, 'Oracle'); ...
1
5128
by: mithunmo | last post by:
If I run the below program . I get the following error message Can't call method "value" on an undefined value at C:/Perl/site/lib/Win32/IE/Mec hanize.pm line 900. Please let me know your inputs on the same #!c:\perl\bin\perl.exe use strict;
0
7760
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7679
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7996
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8049
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6392
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5284
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3723
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.