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

Return XRecord Names From Dictionary

6 REPLIES 6
Reply
Message 1 of 7
Hugh_Compton
658 Views, 6 Replies

Return XRecord Names From Dictionary

I am attempting to iterate through a dictionary under the NOD and return the name of each XRecord held there. I have attached the code which shows where I am stuck. This should be easy - please can someone help 🙂
6 REPLIES 6
Message 2 of 7

My dictionary code always has the key when I get data, so I had to look into this a little.

It appears that whether you use a for each loop, or an enumerator, the type is DBDictionaryEntry, not Xrecord.

Try this:

Dim Entry As DBDictionaryEntry

For Each Entry In oDict

'entry.key

'entry.value

Next

Dave O.                                                                  Sig-Logos32.png
Message 3 of 7
Anonymous
in reply to: Hugh_Compton



If what you need is the names of all entries in a
DBDictionary, you

don't have to use 'for each' or an IEnumerator, you can just
cast

the DBDictionary to an IDictionary, and the names are in
the Keys

property:

 

{code}

 

Public Sub GetDictKeys()
    Dim db As
Database = HostApplicationServices.WorkingDatabase
    Dim tr
As Transaction = db.TransactionManager.StartTransaction
   
Dim NOD As DBDictionary

    Dim idNod =
db.NamedObjectsDictionaryId

    NOD = TryCast(
tr.GetObject(idNod, OpenMode.ForRead, False),
DBDictionary)
    If Not (NOD Is Nothing)
Then
        Dim dictKey As String =
"STATENAMES"
        If
NOD.Contains(dictKey) Then
       
    Dim dbDict As DBDictionary


size=2>            Dim
idDict As ObjectId = NOD.GetAt(dictKey)

           
dbDict = TryCast(tr.GetObject( idDict, OpenMode.ForRead),
DBDictionary)
           
If Not (dbDict Is Nothing)
Then
               
Dim dictionary As IDictionary =
dbDict
               
Dim keys As ICollection =
dictionary.Keys
               
Dim name As
String
               
For Each name In
keys
                   
Console.WriteLine(name)
               
Next
            End
If
        End If
   
End If
End Sub

{code}


 

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000
through 2009

href="http://www.acadxtabs.com">http://www.acadxtabs.com

 


 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I
am attempting to iterate through a dictionary under the NOD and return the
name of each XRecord held there. I have attached the code which shows where I
am stuck. This should be easy - please can someone help 🙂
Message 4 of 7
Anonymous
in reply to: Hugh_Compton

Let's try that again:

{code}

Public Sub GetDictKeys()
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim tr As Transaction = db.TransactionManager.StartTransaction
Dim NOD As DBDictionary
Dim idNod = db.NamedObjectsDictionaryId
NOD = TryCast( tr.GetObject(idNod, OpenMode.ForRead, False), DBDictionary)
If Not (NOD Is Nothing) Then
Dim dictKey As String = "STATENAMES"
If NOD.Contains(dictKey) Then
Dim dbDict As DBDictionary
Dim idDict As ObjectId = NOD.GetAt(dictKey)
dbDict = TryCast(tr.GetObject( idDict, OpenMode.ForRead), DBDictionary)
If Not (dbDict Is Nothing) Then
Dim dictionary As IDictionary = dbDict
Dim keys As ICollection = dictionary.Keys
Dim name As String
For Each name In keys
Console.WriteLine(name)
Next
End If
End If
End If
End Sub

{code}


--

http://www.caddzone.com

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

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


wrote in message news:6058438@discussion.autodesk.com...
I am attempting to iterate through a dictionary under the NOD and return the name of each XRecord held there. I have attached the code which shows where I am stuck. This should be easy - please can someone help 🙂
Message 5 of 7
Hugh_Compton
in reply to: Hugh_Compton

That works great, thanks Tony. Now that I have the name, how would I get the corresponding key?

My attempt:
For Each kvp As KeyValuePair(Of String, String) In dictionary
MsgBox("Key= " + kvp.Key + vbNewLine + "Value= " + kvp.Value)
Next kvp

This returns error "Specified cast is not valid" 😞

ChiefBrainCloud - couldn't seem to make your example key.value equal to the name of my XRecord 😞
Message 6 of 7
Anonymous
in reply to: Hugh_Compton


You need to post more complete code, as I have no idea

what the 'dictionary'
size=2>variable's type is in your snippet.

 

Regardless, you don't use KeyValuePair here,
because

that isn't the element type of either IDictionary or
the

DBDictionary enumerator.

 

If you have the key (e.g., the name), you get the
value

(an ObjectId) using the Item() method.


 

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000
through 2009

href="http://www.acadxtabs.com">http://www.acadxtabs.com

 


 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
That
works great, thanks Tony. Now that I have the name, how would I get the
corresponding key?

My attempt:
For Each kvp As KeyValuePair(Of
String, String) In dictionary
MsgBox("Key= " + kvp.Key + vbNewLine +
"Value= " + kvp.Value)
Next kvp

This returns error "Specified cast
is not valid" 😞

ChiefBrainCloud - couldn't seem to make your example
key.value equal to the name of my XRecord 😞
Message 7 of 7
Hugh_Compton
in reply to: Hugh_Compton

Sorry - my fault. I had my definitions a little mixed up. Tony summed them up perfectly in his last sentence.

Thank you both for your help.

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