<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic System.AccessViolationException while trying to get  layername in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495547#M11220</link>
    <description>&lt;P&gt;Hello, I am getting "&lt;SPAN&gt;System.AccessViolationException: Attempted to read or write protected memory.&lt;/SPAN&gt;" while trying go get layer of blockref in array.&lt;/P&gt;&lt;P&gt;Here is part of the code that give exeption, it always gives it then i try to get (obj as BlockReference).Layer, if i remove it, it works fine:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;                    if (AssocArray.IsAssociativeArray(objectId)) 
                    {
                        using (BlockReference br = objectId.Open(OpenMode.ForRead) as BlockReference)
                        {
                            using (DBObjectCollection brs = new DBObjectCollection())
                            {
                                br.Explode(brs);
                                if (brs != null &amp;amp;&amp;amp; brs.Count &amp;gt; 0)
                                {
                                    foreach (DBObject obj in brs)
                                    {
                                        for (int j = 0; j &amp;lt; laylistBL.Length; j++)
                                        {
                                            if (obj is BlockReference &amp;amp;&amp;amp; (obj as BlockReference).Layer == laylistBL[j])
                                            {
                                                TableValues[12 + j] += 1;
                                            }
                                            obj.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                    }&lt;/LI-CODE&gt;&lt;P&gt;I found similar code and modified it for myself&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/net/getting-block-name-from-associative-array/td-p/8479018" target="_blank" rel="noopener"&gt;here.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Can you please point out what I'm doing wrong?&lt;/P&gt;</description>
    <pubDate>Thu, 20 Oct 2022 08:57:01 GMT</pubDate>
    <dc:creator>r00teniy</dc:creator>
    <dc:date>2022-10-20T08:57:01Z</dc:date>
    <item>
      <title>System.AccessViolationException while trying to get  layername</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495547#M11220</link>
      <description>&lt;P&gt;Hello, I am getting "&lt;SPAN&gt;System.AccessViolationException: Attempted to read or write protected memory.&lt;/SPAN&gt;" while trying go get layer of blockref in array.&lt;/P&gt;&lt;P&gt;Here is part of the code that give exeption, it always gives it then i try to get (obj as BlockReference).Layer, if i remove it, it works fine:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;                    if (AssocArray.IsAssociativeArray(objectId)) 
                    {
                        using (BlockReference br = objectId.Open(OpenMode.ForRead) as BlockReference)
                        {
                            using (DBObjectCollection brs = new DBObjectCollection())
                            {
                                br.Explode(brs);
                                if (brs != null &amp;amp;&amp;amp; brs.Count &amp;gt; 0)
                                {
                                    foreach (DBObject obj in brs)
                                    {
                                        for (int j = 0; j &amp;lt; laylistBL.Length; j++)
                                        {
                                            if (obj is BlockReference &amp;amp;&amp;amp; (obj as BlockReference).Layer == laylistBL[j])
                                            {
                                                TableValues[12 + j] += 1;
                                            }
                                            obj.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                    }&lt;/LI-CODE&gt;&lt;P&gt;I found similar code and modified it for myself&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/net/getting-block-name-from-associative-array/td-p/8479018" target="_blank" rel="noopener"&gt;here.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Can you please point out what I'm doing wrong?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 08:57:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495547#M11220</guid>
      <dc:creator>r00teniy</dc:creator>
      <dc:date>2022-10-20T08:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: System.AccessViolationException while trying to get  layername</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495648#M11221</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if you iterate through the BlockTableRecord entities of the BlockReference instead of exploding it?&lt;/P&gt;
&lt;P&gt;Something like this (not tested).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;if (AssocArray.IsAssociativeArray(objectId)) 
{
    using (BlockReference br = (BlockReference)objectId.Open(OpenMode.ForRead))
    {
        using (BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.Open(OpenMode.ForRead))
        {
            foreach (objectId id in btr)
            {
                if (id.ObjectClass.Name = "AcDbBlockReference")
                {
                    using(BlockReference bRef = (BlockReference)id.Open(OpenMode.ForRead))
                    {
                        for (int j = 0; j &amp;lt; laylistBL.Length; j++)
                        {
                            if (bRef.Layer == laylistBL[j])
                            {
                                TableValues[12 + j] += 1;
                            }
                        }
                    }
                }
            }
        }
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 10:22:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495648#M11221</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-10-20T10:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: System.AccessViolationException while trying to get  layername</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495708#M11222</link>
      <description>&lt;P&gt;Thank you, will try to work in this direction.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 10:26:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495708#M11222</guid>
      <dc:creator>r00teniy</dc:creator>
      <dc:date>2022-10-20T10:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: System.AccessViolationException while trying to get  layername</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495748#M11223</link>
      <description>&lt;P&gt;Oops!...&lt;/P&gt;
&lt;P&gt;I forgot casting the BlockTableRecord.&lt;/P&gt;
&lt;P&gt;I corrected the upper code replacing:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using (BlockTableRecord btr = br.BlockTableRecord.Open(OpenMode.ForRead))&lt;/LI-CODE&gt;
&lt;P&gt;with:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using (BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.Open(OpenMode.ForRead))&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 20 Oct 2022 10:25:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495748#M11223</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-10-20T10:25:27Z</dc:date>
    </item>
    <item>
      <title>Re: System.AccessViolationException while trying to get  layername</title>
      <link>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495807#M11224</link>
      <description>&lt;P&gt;For me this line did it, thank you:&lt;BR /&gt;using (BlockTableRecord btr = br.DynamicBlockTableRecord.Open(OpenMode.ForRead) as BlockTableRecord)&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 10:42:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/system-accessviolationexception-while-trying-to-get-layername/m-p/11495807#M11224</guid>
      <dc:creator>r00teniy</dc:creator>
      <dc:date>2022-10-20T10:42:50Z</dc:date>
    </item>
  </channel>
</rss>

