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