dict.GetAt error return code?

dict.GetAt error return code?

Anonymous
Not applicable
1,172 Views
9 Replies
Message 1 of 10

dict.GetAt error return code?

Anonymous
Not applicable
Can we test for a key in a dictionary without using the IDictionary
Enumerator and testing the whole dictionary. I was hoping for a retun
code similar to ARX eKeyNotFound when using dict.GetAt but this
just throws a system error when not found.

Thanks!
0 Likes
1,173 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
Here's what I'm using - actually very quick.
[code]
static public bool HasKey(DBDictionary dbDict, string dictKey)
{

foreach (DictionaryEntry de in dbDict)
{

if (de.Key.ToString() == dictKey)
return true;
}

return false;
}
[/code]
"Paul Richardson"

wrote in message
news:5683828@discussion.autodesk.com...
Can we test for a key in a dictionary without using the IDictionary
Enumerator and testing the whole dictionary. I was hoping for a retun
code similar to ARX eKeyNotFound when using dict.GetAt but this
just throws a system error when not found.

Thanks!

0 Likes
Message 3 of 10

Anonymous
Not applicable
How about this code

static public bool HasKey(DBDictionary dbDict, string dictKey)
{
try
{
ObjectId id = dbDict.GetAt(dictKey);
return true;
}
catch
{
return false;
}
}
0 Likes
Message 4 of 10

Anonymous
Not applicable
I try to keep my trys for the unexpected but since it's an IDict it's
either this or the loop. Thanks.
wrote in message news:5684537@discussion.autodesk.com...
How about this code

static public bool HasKey(DBDictionary dbDict, string dictKey)
{
try
{
ObjectId id = dbDict.GetAt(dictKey);
return true;
}
catch
{
return false;
}
}
0 Likes
Message 5 of 10

Anonymous
Not applicable
Paul - What's wrong with the Contains() method ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message news:5684696@discussion.autodesk.com...
I try to keep my trys for the unexpected but since it's an IDict it's
either this or the loop. Thanks.
wrote in message news:5684537@discussion.autodesk.com...
How about this code

static public bool HasKey(DBDictionary dbDict, string dictKey)
{
try
{
ObjectId id = dbDict.GetAt(dictKey);
return true;
}
catch
{
return false;
}
}

0 Likes
Message 6 of 10

Anonymous
Not applicable
Nothing wrong with Contains() it's the coder...:) Thanks!
"Tony Tanzillo" wrote in message
news:5685719@discussion.autodesk.com...
Paul - What's wrong with the Contains() method ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message
news:5684696@discussion.autodesk.com...
I try to keep my trys for the unexpected but since it's an IDict it's
either this or the loop. Thanks.
wrote in message news:5684537@discussion.autodesk.com...
How about this code

static public bool HasKey(DBDictionary dbDict, string dictKey)
{
try
{
ObjectId id = dbDict.GetAt(dictKey);
return true;
}
catch
{
return false;
}
}

0 Likes
Message 7 of 10

Anonymous
Not applicable
Just a point of clarification.

If you are testing to see if a key exists, and
if it does, retrieving the value, then don't use
Contains() followed by GetAt(), like this:

DBDictionary dict = // ...

if( dict.Contains("mykey") )
{
ObjectId id = dict.GetAt("mykey");
// use id
}

The reason this is not the preferred method, is
because it requires two lookups, rather than one.

Here is how I would do it:

DBDictionary dict = // ........
ObjectId id = ObjectId.Null;
try
{
dict.GetAt("mykey");
}
catch {}

if( ! id.IsNull )
{
// use id here.
}

The reason I would take this approach is because
it allows me to get an existing value with only one
lookup rather than two (e.g., Contains() does one,
and GetAt() does another lookup).

Yes, it uses exception handling.

Yes, if your dictionary only has a handful of items
in it, it'll have no noticeable difference.

But, for the purpose of good, efficient coding and
scalable performance, in this case, using exception
handling allows you to get an existing value from
a dictionary with only one lookup, rather than two.

So, if someone has been preaching that without
condition, you should limit the use of use try/catch
to trapping unexpected errors, they're mistaken.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message news:5685742@discussion.autodesk.com...
Nothing wrong with Contains() it's the coder...:) Thanks!
"Tony Tanzillo" wrote in message
news:5685719@discussion.autodesk.com...
Paul - What's wrong with the Contains() method ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message
news:5684696@discussion.autodesk.com...
I try to keep my trys for the unexpected but since it's an IDict it's
either this or the loop. Thanks.
wrote in message news:5684537@discussion.autodesk.com...
How about this code

static public bool HasKey(DBDictionary dbDict, string dictKey)
{
try
{
ObjectId id = dbDict.GetAt(dictKey);
return true;
}
catch
{
return false;
}
}

0 Likes
Message 8 of 10

Anonymous
Not applicable
Thank you Tony - Nice explanation~
"Tony Tanzillo" wrote in message
news:5685995@discussion.autodesk.com...
Just a point of clarification.

If you are testing to see if a key exists, and
if it does, retrieving the value, then don't use
Contains() followed by GetAt(), like this:

DBDictionary dict = // ...

if( dict.Contains("mykey") )
{
ObjectId id = dict.GetAt("mykey");
// use id
}

The reason this is not the preferred method, is
because it requires two lookups, rather than one.

Here is how I would do it:

DBDictionary dict = // ........
ObjectId id = ObjectId.Null;
try
{
dict.GetAt("mykey");
}
catch {}

if( ! id.IsNull )
{
// use id here.
}

The reason I would take this approach is because
it allows me to get an existing value with only one
lookup rather than two (e.g., Contains() does one,
and GetAt() does another lookup).

Yes, it uses exception handling.

Yes, if your dictionary only has a handful of items
in it, it'll have no noticeable difference.

But, for the purpose of good, efficient coding and
scalable performance, in this case, using exception
handling allows you to get an existing value from
a dictionary with only one lookup, rather than two.

So, if someone has been preaching that without
condition, you should limit the use of use try/catch
to trapping unexpected errors, they're mistaken.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message
news:5685742@discussion.autodesk.com...
Nothing wrong with Contains() it's the coder...:) Thanks!
"Tony Tanzillo" wrote in message
news:5685719@discussion.autodesk.com...
Paul - What's wrong with the Contains() method ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message
news:5684696@discussion.autodesk.com...
I try to keep my trys for the unexpected but since it's an IDict it's
either this or the loop. Thanks.
wrote in message news:5684537@discussion.autodesk.com...
How about this code

static public bool HasKey(DBDictionary dbDict, string dictKey)
{
try
{
ObjectId id = dbDict.GetAt(dictKey);
return true;
}
catch
{
return false;
}
}

0 Likes
Message 9 of 10

dgorsman
Consultant
Consultant
Thanks, Tony - I might be able to use this in a few other areas to clean up some machete-code I inherited.
----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 10 of 10

Anonymous
Not applicable
>> Here is how I would do it:

Oops. Sorry, that should have been:

DBDictionary dict = // ........
ObjectId id = ObjectId.Null;
try
{
id = dict.GetAt("mykey");
}
catch {}

if( ! id.IsNull )
{
// use id here.
}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Tony Tanzillo" wrote in message news:5685995@discussion.autodesk.com...
Just a point of clarification.

If you are testing to see if a key exists, and
if it does, retrieving the value, then don't use
Contains() followed by GetAt(), like this:

DBDictionary dict = // ...

if( dict.Contains("mykey") )
{
ObjectId id = dict.GetAt("mykey");
// use id
}

The reason this is not the preferred method, is
because it requires two lookups, rather than one.

Here is how I would do it:

DBDictionary dict = // ........
ObjectId id = ObjectId.Null;
try
{
dict.GetAt("mykey");
}
catch {}

if( ! id.IsNull )
{
// use id here.
}

The reason I would take this approach is because
it allows me to get an existing value with only one
lookup rather than two (e.g., Contains() does one,
and GetAt() does another lookup).

Yes, it uses exception handling.

Yes, if your dictionary only has a handful of items
in it, it'll have no noticeable difference.

But, for the purpose of good, efficient coding and
scalable performance, in this case, using exception
handling allows you to get an existing value from
a dictionary with only one lookup, rather than two.

So, if someone has been preaching that without
condition, you should limit the use of use try/catch
to trapping unexpected errors, they're mistaken.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message news:5685742@discussion.autodesk.com...
Nothing wrong with Contains() it's the coder...:) Thanks!
"Tony Tanzillo" wrote in message
news:5685719@discussion.autodesk.com...
Paul - What's wrong with the Contains() method ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Paul Richardson"

wrote in message
news:5684696@discussion.autodesk.com...
I try to keep my trys for the unexpected but since it's an IDict it's
either this or the loop. Thanks.
wrote in message news:5684537@discussion.autodesk.com...
How about this code

static public bool HasKey(DBDictionary dbDict, string dictKey)
{
try
{
ObjectId id = dbDict.GetAt(dictKey);
return true;
}
catch
{
return false;
}
}

0 Likes