.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

dict.GetAt error return code?

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
416 Views, 9 Replies

dict.GetAt error return code?

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!
9 REPLIES 9
Message 2 of 10
Anonymous
in reply to: Anonymous

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!

Message 3 of 10
Anonymous
in reply to: Anonymous

How about this code

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

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;
}
}
Message 5 of 10
Anonymous
in reply to: Anonymous

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;
}
}

Message 6 of 10
Anonymous
in reply to: Anonymous

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;
}
}

Message 7 of 10
Anonymous
in reply to: Anonymous

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;
}
}

Message 8 of 10
Anonymous
in reply to: Anonymous

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;
}
}

Message 9 of 10
dgorsman
in reply to: Anonymous

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.


Message 10 of 10
Anonymous
in reply to: Anonymous

>> 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;
}
}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost