Could an object be equal to null ?

Could an object be equal to null ?

J-Rocks
Collaborator Collaborator
2,266 Views
10 Replies
Message 1 of 11

Could an object be equal to null ?

J-Rocks
Collaborator
Collaborator

Hi,

 

I have seen in many programs programmers checking out if an object is not equal to null then proceed to next line .

 

Is it possible for example in the below codes the object be equal to null ? if not , why programmers using this check out then ?

 

thanks in advance.

 

if (per.Status == PromptStatus.OK)
                {
                using (Transaction MyTrans = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        BlockReference Myblk = (BlockReference)MyTrans.GetObject(per.ObjectId, OpenMode.ForRead);
                        if (Myblk != null)
                        {
0 Likes
Accepted solutions (4)
2,267 Views
10 Replies
Replies (10)
Message 2 of 11

SENL1362
Advisor
Advisor
Accepted solution

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:

Circle cr=tr.getobject(someId, OpenMode.ForRead) as Circle;

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.

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.

This construction is often used as an alternative to a Switch statement when there are limited choices.

 

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,  cr.Center, you are in big trouble when cr IS NULL.

 

In the next version of C this is a short cut to prevent these situations: cr?.Center

See also nullable types: bool? flag=null;

 

The alternative for a Switch when limited choices:

for instance in the next sample w're only interested in a circle

foreach(ObjectId in someBlockTableRecord)

{

    Circle cr=tr.getobject(someId, OpenMode.ForRead) as Circle;

    if (cr != null)  

    {

         //process circle

   

 

Message 3 of 11

J-Rocks
Collaborator
Collaborator

Thank you .

 

it is a little bit confusing to me to get it right , sorry.

 

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.

 

 

0 Likes
Message 4 of 11

FRFR1426
Collaborator
Collaborator

In your case, the variable Myblk can not be null. If the Id is not valid GetObject will throw an exception. And the cast to BlockReference will also throw an exception if it fails, so the null check should be removed to improve readability 

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 5 of 11

J-Rocks
Collaborator
Collaborator

Thank you.

 

My first example was just to descibe my idea .

 

Should I check if the line entity is not equal to NULL in this example ?  and why ?

 

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;
                      if (ent == null)
0 Likes
Message 6 of 11

SENL1362
Advisor
Advisor
not required
0 Likes
Message 7 of 11

FRFR1426
Collaborator
Collaborator
Accepted solution

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).

 

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);
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 8 of 11

jeff
Collaborator
Collaborator
Accepted solution

It's not you have a empty object, its the variable does not point to a object.

 

As FRFR1426 pointed out If your looking at examples where the as operator is being used to check an object's type then checking the result for null is needed to verify it.

                         

 

 

GetObject() -> returns type Entity.

So this might be a a workflow seen getting lines from model space

 

            foreach (var obj in ModelSpace)
            {
                Line ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
                  if (ent == null)
            }

 

Here what i do using library from here and mentioned here

 

                var modelSpace = Db.ModelSpace();
                foreach (var line in modelSpace.GetEntities<Line>())
                {
                    //////do what needed
                }
You can also find your answers @ TheSwamp
Message 9 of 11

J-Rocks
Collaborator
Collaborator

THANK YOU ALL.

 

Can I come up with a believe that says :

If I get the object with (CAST) builder and the object comes from a selection set with a filter with a specific entity names < say lines > or by a PromptEntityResult wouldn't needed using to get sure if that object is not equal to NULL ?

 

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 <Line> ) and the object might be a Circle , Text ... etc and not the type one that I want to get ?

 

 

0 Likes
Message 10 of 11

_gile
Consultant
Consultant
Accepted solution

Yes.

 

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:

foreach (ObjectId id in modelSpace)
{
    Line line = tr.GetObject(id, OpenMode.ForRead) as Line;
    if (line != null)
    {
        // ...
    }
}

 

But since AutoCAD 2009, if I remember correctly, you can determine the object type of an ObjectId without opening it (with the ObjectClass property) and, then, use the 'direct cast' operator, which is much more efficient.

foreach (ObjectId id in modelSpace)
{
    if (id.ObjectClass == RXObject.GetClass(typeof(Line)))
    {
        Line line = (Line)tr.GetObject(id, OpenMode.ForRead);
        // ...
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 11

J-Rocks
Collaborator
Collaborator

Thanks gile for your explanations.

0 Likes