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

instantiate a class question.

17 REPLIES 17
Reply
Message 1 of 18
junoj
928 Views, 17 Replies

instantiate a class question.

 

Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

 

Hi everyone, I am trying to understand the code above. It’s from the API Training Lab. The above is an example of how to instantiate an instance of the editor class.

 

First of all, how is this even possible, they are both from different class? Also shouldn’t one use ‘NEW’ to instantiate a class?

Please help, because this is really confusing. . . .Smiley Indifferent

 

Thank you in advance,

 

-Jay

17 REPLIES 17
Message 2 of 18
junoj
in reply to: junoj

Hi everyone,

I have been digging all day trying to find an answer to my question. This is what I came up with; please correct me if I am wrong:

 

Autodesk.AutoCAD.EditorInput.Editor = is a class

 

Autodesk.AutoCAD.ApplicaionServices.Applicaion= is a class

DocumentManager = is a property of Applicaion class which gets the DocumentCollection class

MdiActiveDocument = is a property of Document Collection class which gets the Document class

Editor = is a property of the Document class which gets the Editor class located in . . . EditorInput.Editor

 

So ed becomes a variable that holds the pointer to the Editor of the ActiveDocument?!?!? Smiley Indifferent

 

I think my brain has melted . . . .

 

Anyhow, please advice. . .

 

-J

Message 3 of 18
caddzone
in reply to: junoj

Right.

 

Each Document has its own instance of an Editor object, which

is accessed via the Document's Editor property.

 

Those are not classes you create, you only get an

existing instance of one through a property of its 'owner',

which in the case of the Editor class, is the Document.



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


Message 4 of 18
Rudedog2
in reply to: caddzone

Contributer caddzone is correct. 

 

It might just look a little strange because of the lengthy type names.  Try this one for size.  It is functionally equivalent.

 

    public class Class5
    {
        internal Class5()
        {
            //this constructor is only available internally.
        }
    }

    public class API_ForClass5
    {
        private static Class5 instance;
        public static Class5 Instance
        {
            get
            {
                return instance;
            }
        }
        public API_ForClass5()
        {
            instance = new Class5();
        }
    }

    public class Consumer
    {
        void Method()
        {
            API_ForClass5 application = new API_ForClass5();
            Class5 myInstance = API_ForClass5.Instance;
        }
    }

 

 

Rudy   =8^D

Fooling computers since 1971. Mark best replies as answers.
Message 5 of 18
Rudedog2
in reply to: Rudedog2

Almost forgot.  You may wish to investigate what is known as a Singleton.  Find info at this link.

 

http://www.dofactory.com/Patterns/PatternSingleton.aspx

 

It is a way of guaranteeing that there is one and only one instance of a class over the lifetime of an application.  In this case, the "lifetime" of interest is the Editor associated with the application running in the Mdi Child Window.

 

Hope this helps unlock your brain lock.

 

Rudy   =8^D

 

 

Fooling computers since 1971. Mark best replies as answers.
Message 6 of 18
Jeffrey_H
in reply to: Rudedog2

I code in VB so sorry if I am not understanding your code right, but does it not need to check if the the instant is null before it creates a new one.

You can also find your answers @ TheSwamp
Message 7 of 18
Jeffrey_H
in reply to: Jeffrey_H

And how can create a new instance outside the class when the constructor is private or internal

As I said I code in VB so it might be different

You can also find your answers @ TheSwamp
Message 8 of 18
caddzone
in reply to: Jeffrey_H

You're correct for pointing that out.

 

In the Singleton Pattern, the instance is usuallly created when the private member is

declared, or creation is deferred until the instance is first requested.

 

So:

 

public class MySingleton
{
   // The constructor should be private, not 'internal'.
   MySingleton()
   {
   }

   // defer creation until the instance 
   // is actually requested:

   private static instance = null;

   // Public access to the single instance:
   public static Instance
   {
      get
      {
         if( instance == null )
            instance = new MySingleton();
         return instance;
      }
   }
}

 



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


Message 9 of 18
caddzone
in reply to: Jeffrey_H


@fro2001 wrote:

And how can create a new instance outside the class when the constructor is private or internal

As I said I code in VB so it might be different


If you're talking about the Singleton pattern, you cannot create instances of

singletons directly, otherwise the class would not be able to restrict the

number of instances to one.

 

The singleton pattern enforces the restriction that there can be only a single

instance of an object. So, an object that uses the singleton pattren cannot

allow instances to be created by any code.

 



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


Message 10 of 18
caddzone
in reply to: Rudedog2

I'm not sure what singletons have to do with the original question,

none of the objects the OP mentioned (Document, Editor, etc)

use the singleton pattern.

 

The OP's confusion appeared to be in how instances of some

types cannot be created directly, but rather can only be acquired

by calling an API, but that doesn't make them singletons.

 

 



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


Message 11 of 18
Jeffrey_H
in reply to: caddzone

Sorry I meant don't you have to have a Public  static or shared method inside a class to access it if you make the constructor privite or internal

 

From the code above

 instance = new Class5();

 

Would that fail since Class5 constructor is internal  

You can also find your answers @ TheSwamp
Message 12 of 18
caddzone
in reply to: Jeffrey_H

Sorry, not sure what you're referring to by 'the code above', and

I'm not sure I understand your question.



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


Message 13 of 18
Jeffrey_H
in reply to: caddzone

  public class Class5
    {
        internal Class5()
        {
            //this constructor is only available internally.
        }
    }

    public class API_ForClass5
    {
        private static Class5 instance;
        public static Class5 Instance
        {
            get
            {
                return instance;
            }
        }
        public API_ForClass5()
        {
            instance = new Class5();
        }
    }

    public class Consumer
    {
        void Method()
        {
            API_ForClass5 application = new API_ForClass5();
            Class5 myInstance = API_ForClass5.Instance;
        }
    }

 

 

 

 

In the constructor for API_ForClass5()  when it tries to set "instance" to a new instance of Class5

Would it fail since Class5 constructors is internal

 

 

 

 

 

 

 

You can also find your answers @ TheSwamp
Message 14 of 18
junoj
in reply to: Jeffrey_H

What a brain teaser. . . .  I finally got it, take a look at the attached image it should make it clear a bit.

 

So the point of all of this is to be able to get the latest Class5 created. .

 

 

284i411103CD6180634F

 

 

 

 

Message 15 of 18
caddzone
in reply to: Jeffrey_H

You should probably read up on static verses instance members.

 

The code you show creates an instance of one class

(API_ForClass5), and calls a static API of that class, to

get an instance of another class (Class5).

 

You don't need to create an instance of a class to use

a static API of that class.

 

About your question, 'internal' visiblity means that a

member or class is visible to any code in the same

assembly, so if those two classes are in the same

assembly, it works.

 

My suggestion for anyone that's just starting out with the

AutoCAD managed API, is that they try to focus more on

the basic concepts of the language/runtime, like scope

and visiblity as they relate to 'static', 'internal' and so on

(the 'horse'), before trying to learn the AutoCAD managed

API (the 'cart').

 

 



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


Message 16 of 18
Rudedog2
in reply to: caddzone

 


@caddzone wrote:

You should probably read up on static verses instance members.

 

The code you show creates an instance of one class

(API_ForClass5), and calls a static API of that class, to

get an instance of another class (Class5).

 

You don't need to create an instance of a class to use

a static API of that class.

 

About your question, 'internal' visiblity means that a

member or class is visible to any code in the same

assembly, so if those two classes are in the same

assembly, it works.

 

My suggestion for anyone that's just starting out with the

AutoCAD managed API, is that they try to focus more on

the basic concepts of the language/runtime, like scope

and visiblity as they relate to 'static', 'internal' and so on

(the 'horse'), before trying to learn the AutoCAD managed

API (the 'cart').

 

 


 

 

@Tony

 

I see my code snippet has caused some confusion.  I think the OP finally understood what it trying to illustrate.  The intent was to demonstrate one way the code pattern for obtaining an instance of the Editor might look.  What clases could like to force the consumer to reference a property in another class just ot obtain an instance of another.

 

I only mentioned Singleton because it is what I would have implemented internally, not wanting to rely on every one on the developer team being on the same page when it came to "Rules of Engagement" for the use of Class5.  Someone might forget that only instance should exist throughout the lifetime of another object.

 

@junoj:

Congratulations on "getting it", as you put it.  The code snippet that I posted---and which you re-posted---is a bit subtle and is not a true Singleton as Tony had pointed out.  Just to be clear, here is what my intent was.

 

The first two classes would appear in an "API Assembly", while the 3rd class would be code written by a consumer like you the developer.  Consumers cannot create instances of Class5.  Any Class5 instance is created internally by the types defined within the API.  So, a true Singleton is not needed.  Although it wouldn't hurt to create one just in case.

 

In most cases, a Singleton is a self-contained class.  The only instances are created are by Singleton class itself.  My code snippet uses some "other class" to create instances, which are assigned to a static property witin the class. 

 

If were writing the code, I would not allow consumers to create instances of this "other class" at all.  I would have Factory Methods and/or Abstract Object Factory classes---the other class mentioned above---to set up and initialize the rather complex type, Application.  You can read more about Object Factories, the GoF, and other OOP basics here.

 

http://www.dofactory.com/Patterns/Patterns.aspx

 

Rudy

Fooling computers since 1971. Mark best replies as answers.
Message 17 of 18
junoj
in reply to: Jeffrey_H

Thank you Rudy and everyone else.

 

This has been a very intriguing learning experience, considering that I have no C# or OOP experience. I am now starting to see a bigger picture to OOP.

 

Again, Thank you,

 

-Jay

Message 18 of 18
caddzone
in reply to: Rudedog2

The confusion caused by your snippet is that you create an instance of

'API_ForClass5' and then use a static member of the class.

 

There is point or purpose to creating an instance of a class, to use

a static member, and yes, that very much confuses whatever it was

you were trying to get across about singletons.

 



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


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