<?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 Re : Could an object be equal to null ? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977448#M37510</link>
    <description>&lt;P&gt;In your case, the variable&amp;nbsp;Myblk can not be null. If the Id is not valid GetObject will throw an exception. And the cast to&amp;nbsp;BlockReference will also throw an exception if it fails, so the null check should&amp;nbsp;be removed to improve readability&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Jan 2016 13:11:58 GMT</pubDate>
    <dc:creator>FRFR1426</dc:creator>
    <dc:date>2016-01-07T13:11:58Z</dc:date>
    <item>
      <title>Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977019#M37507</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have seen in many programs programmers checking out if an object is not equal to null then proceed to next line .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible for example in the below codes the object be equal to null ? if not , why programmers using this check out then ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if (per.Status == PromptStatus.OK)
                {
                using (Transaction MyTrans = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        BlockReference Myblk = (BlockReference)MyTrans.GetObject(per.ObjectId, OpenMode.ForRead);
                        if &lt;FONT color="#FF0000"&gt;(Myblk != null)&lt;/FONT&gt;
                        {&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jan 2016 06:36:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977019#M37507</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-07T06:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977054#M37508</link>
      <description>&lt;P&gt;You're example may not result in a NULL value and if the ID is not a BlockReference it may even throw an exception, but this one sure can:&lt;/P&gt;&lt;P&gt;Circle cr=tr.getobject(someId, OpenMode.ForRead) as Circle;&lt;/P&gt;&lt;P&gt;The ID may not be valid, such as marked for Delete or Disposed or not yet Comitted etc. and that also may result in a NULL value fot the BlockReference. You never know so testing may be essential.&lt;/P&gt;&lt;P&gt;The above prevents the Throw which is an expensive action -- in time context, when the someId not belong to a Circle but to a different object.&lt;/P&gt;&lt;P&gt;This construction is often used as an alternative to a Switch statement when there are limited choices.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In general testing for null is good practice and relatively cheap so use it as much as possible, especially when a property of the object will be used, such as in,&amp;nbsp; cr.Center, you are in big trouble when cr IS NULL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the next version of C this is a short cut to prevent these situations: cr?.Center&lt;/P&gt;&lt;P&gt;See also nullable types: bool? flag=null;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The alternative for a Switch when limited choices:&lt;/P&gt;&lt;P&gt;for instance in the next sample w're only interested in a circle&lt;/P&gt;&lt;P&gt;foreach(ObjectId in someBlockTableRecord)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Circle cr=tr.getobject(someId, OpenMode.ForRead) as Circle;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (cr != null)&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //process circle&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2016 07:24:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977054#M37508</guid>
      <dc:creator>SENL1362</dc:creator>
      <dc:date>2016-01-07T07:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977441#M37509</link>
      <description>&lt;P&gt;Thank you .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it is a little bit confusing to me to get it right , sorry.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assume that I asked the user to select objects then the program would run on the same quantity of the selection sets then how could objects in the selection set would be in a way or another be equal to null ? this is entirely unreasonable to me at least.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2016 13:10:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977441#M37509</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-07T13:10:06Z</dc:date>
    </item>
    <item>
      <title>Re : Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977448#M37510</link>
      <description>&lt;P&gt;In your case, the variable&amp;nbsp;Myblk can not be null. If the Id is not valid GetObject will throw an exception. And the cast to&amp;nbsp;BlockReference will also throw an exception if it fails, so the null check should&amp;nbsp;be removed to improve readability&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2016 13:11:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977448#M37510</guid>
      <dc:creator>FRFR1426</dc:creator>
      <dc:date>2016-01-07T13:11:58Z</dc:date>
    </item>
    <item>
      <title>Re : Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977462#M37511</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My first example was just to descibe my idea .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should I check if the line entity is not equal to NULL in this example ?&amp;nbsp; and why ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;TypedValue[] tv = new TypedValue [] { new TypedValue(0, "LINE") };
                SelectionFilter fltr = new SelectionFilter(tv);
                PromptSelectionResult ss = edit.GetSelection(fltr);
                if (ss.Status == PromptStatus.OK)
                {
                  foreach (SelectedObject obj in ss.Value)
                    {     
                            Line ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
                      &lt;FONT color="#FF0000"&gt;if (ent == null)&lt;/FONT&gt;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jan 2016 13:21:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977462#M37511</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-07T13:21:27Z</dc:date>
    </item>
    <item>
      <title>Re : Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977530#M37512</link>
      <description>not required</description>
      <pubDate>Thu, 07 Jan 2016 13:57:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977530#M37512</guid>
      <dc:creator>SENL1362</dc:creator>
      <dc:date>2016-01-07T13:57:04Z</dc:date>
    </item>
    <item>
      <title>Re : Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977538#M37513</link>
      <description>&lt;P&gt;In this case, yes because you're using a soft cast (keyword as). A soft cast returns null if the cast fails whereas an hard cast throw an exception. In this specific case, I will use an hard cast because the entity type can only be a line (selection set filters on entity type LINE).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;TypedValue[] tv = new TypedValue [] { new TypedValue((int)DxfCode.Start, "LINE") };
SelectionFilter fltr = new SelectionFilter(tv);
PromptSelectionResult ss = edit.GetSelection(fltr);
if (ss.Status == PromptStatus.OK)
{
  foreach (SelectedObject obj in ss.Value)
  {     
    Line ent = (Line)trans.GetObject(obj.ObjectId, OpenMode.ForRead);&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jan 2016 14:02:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5977538#M37513</guid>
      <dc:creator>FRFR1426</dc:creator>
      <dc:date>2016-01-07T14:02:22Z</dc:date>
    </item>
    <item>
      <title>Re: Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5980807#M37514</link>
      <description>&lt;P&gt;It's not you have a empty object, its the variable does not point to a object.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="username_area"&gt;As FRFR1426 pointed out If your looking at examples where the &lt;STRONG&gt;as&lt;/STRONG&gt; operator is being used to check an object's type then checking the result for null&amp;nbsp;is needed to verify it.&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;GetObject() -&amp;gt; returns type Entity.&lt;/P&gt;&lt;P&gt;So this might be a a workflow seen getting lines from model space&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;            foreach (var obj in ModelSpace)
            {
                Line ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
                  &lt;FONT color="#ff0000"&gt;if (ent == null)&lt;/FONT&gt;
            }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here what i do using library from &lt;A href="https://github.com/Hpadgroup/AcExtensionLibrary" target="_blank"&gt;here&lt;/A&gt;&amp;nbsp;and mentioned &lt;A href="http://www.theswamp.org/index.php?topic=50658.0" target="_blank"&gt;here&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;                var modelSpace = Db.ModelSpace();
                foreach (var line in modelSpace.GetEntities&amp;lt;Line&amp;gt;())
                {
                    //////do what needed
                }&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Jan 2016 06:15:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5980807#M37514</guid>
      <dc:creator>jeff</dc:creator>
      <dc:date>2016-01-09T06:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5981333#M37515</link>
      <description>&lt;P&gt;THANK YOU ALL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can I come up with a believe that says :&lt;/P&gt;&lt;P&gt;If I get the object with (CAST) builder and the object comes from a selection set with a filter with a specific entity names &amp;lt; say lines &amp;gt; or by a PromptEntityResult wouldn't needed using to get sure if that object is not equal to NULL ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And if iterating through objects in blocks , Model , Layout spaces I need to check the object out since the object might not be as per the object type that I am searching for ( for instance &amp;lt;Line&amp;gt; ) and the object might be a Circle , Text ... etc and not the type one that I want to get ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jan 2016 05:02:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5981333#M37515</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-10T05:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5981379#M37516</link>
      <description>&lt;P&gt;Yes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To go a little further, when iterating a collection of ObjectIdS (e.g. a BlockTableRecord), you can, as said upper, open the objects with the as 'try cast' operator and check if the object is non null:&lt;/P&gt;
&lt;PRE&gt;foreach (ObjectId id in modelSpace)
{
    Line line = tr.GetObject(id, OpenMode.ForRead) as Line;
    if (line != null)
    {
        // ...
    }
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class="hps"&gt;But since&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;AutoCAD&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;2009, if&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;I remember correctly&lt;/SPAN&gt;&lt;SPAN&gt;, you can determine&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;the object type of&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;an&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;ObjectId&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;without opening it (with the ObjectClass property) and, then, use the 'direct cast' operator,&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;which is much more&lt;/SPAN&gt; &lt;SPAN class="hps"&gt;efficient.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;foreach (ObjectId id in modelSpace)
{
    if (id.ObjectClass == RXObject.GetClass(typeof(Line)))
    {
        Line line = (Line)tr.GetObject(id, OpenMode.ForRead);
        // ...
    }
}&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Jan 2016 08:55:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5981379#M37516</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-01-10T08:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: Could an object be equal to null ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5981389#M37517</link>
      <description>&lt;P&gt;Thanks gile for your explanations.&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jan 2016 10:22:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/could-an-object-be-equal-to-null/m-p/5981389#M37517</guid>
      <dc:creator>J-Rocks</dc:creator>
      <dc:date>2016-01-10T10:22:40Z</dc:date>
    </item>
  </channel>
</rss>

