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

Any reason to make a class defining commands as static?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
JamesMaeding
1234 Views, 6 Replies

Any reason to make a class defining commands as static?

So I was doing tests proving to myself how to make drawing specific variables, and read Kean's code at:

http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_1.html

 

In his example he makes the test function for the global var as all static:

public static class FirstClass {
		private static int counter = 0;

		[Autodesk.AutoCAD.Runtime.CommandMethod("glob")]
		public static void global() {
			Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
			ed.WriteMessage("\nCounter value is: " + counter++);
		}
	}

 but really the only one that needs to be static is the counter variable. This code does the same thing:

public class FirstClass {
		private static int counter = 0;

		[Autodesk.AutoCAD.Runtime.CommandMethod("glob")]
		public void global() {
			Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
			ed.WriteMessage("\nCounter value is: " + counter++);
		}
	}

 

so what difference would the static word make for the overall class, and the "global" function?

 

It was rather confusing to the issue too.

The fact is, once an assembly is loaded that defines a command, you cannot load the assembly again, and cannot make it only apply to one document.

So commands defined by .net are global by default, so its not like there are dwg specific commands (right?).

 

However, the odd behavior of lispfunctions defined in .net, not working on drawings opened before the drawing where an assembly was first loaded, remains.

Now that is confusing, nut not really related to my question here.

thx


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

6 REPLIES 6
Message 2 of 7
_gile
in reply to: JamesMaeding

Hi,

 

Aa AutoCAD .NET Assembly is loaded per session.

 

A static class is mainly the same as a non-static one except all its members must be static too.

 

As shwon by Kean's example, in AutoCAD, static members of a class have a per session scope, non-static members a per document scope.

When a command method is static it is implicitly called as static method of the class:

FirstClass.Global();

 

If the command method isn't static, it is implicitly call as an instance method as if the first time the command is called in a document a new instance of the class was created in this document:

SecondClass instance = new SecondClass();

instance.Local();

 

Maybe adding some constructors in Kean's examples is self explanatory.

 

    // this class may be static but this isn't mandatory
    public class FirstClass 
    {
        private static int counter = 0;

        // this static constructor is only run only once per session
// the first time the Global() method is called. static FirstClass() { Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage( "\nFirstClass static methods are now available."); } [CommandMethod("glob")] public static void Global() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\nCounter value is: " + counter++); } } public class SecondClass { private int counter = 0; private Editor ed; // this instance constructor is run the first time the Local()
// method is called in each newly activated document. public SecondClass() { ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\nA new instance of SecondClass is created."); } [CommandMethod("loc")] public void Local() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\nCounter value is: " + counter++); } }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 7
JamesMaeding
in reply to: _gile

Hi Gilles,

I appreciate your post, but I think you are simply stating what I did, there seems to be no difference between how acad handles a static class and one not static.

I am asking why.

 

It is of interest to me that Kean mixed in that detail, when the focus was on making a variable static or not.

Part of the reason I ask is Bricscad seems to handle things slightly differently.

It does not seem to let you make a per session variable. The static ones are global, like acad.

The non-static ones act like private variables to a function, really wierd.

 

thanks


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

Message 4 of 7
_gile
in reply to: JamesMaeding

Hi,

 

In C# (this is not related to AutoCAD), all the members (fields, properties, methods) of a static class must be static too so you cannot have instance members in a static class.

A non static class can contain both static and instance members.

This is the only difference between a static and a non static class.

So you can declare as static a class if you want to avoid any instance member in this class.

From what I know, only classes defining extension methods (which must be static methods) must be declared static.

 

This is the same for a command method. If it's declared static, it can only use static members and none 'document instance' ones.

 

On the other hand, static members are initialzed once per session (application level) which is cheaper than the 'per document' initialization.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 7
StephenPreston
in reply to: _gile

Hi James,

 

As Gile intimated, using static or non-static command classes for AutoCAD is simply a design choice. As long as you understand the difference between static and non-static, then these are simply two paths to the same destination. Making commands non-static may be preferable if you want a new instance of your command class created for each document in which you invoke the command (which allows you to use instance variables - which can contain different values for different documents). But you can do the same with static classes and store your per document data in Document.UserData (for example). Using static commands can be a bit of a pain because you have to ensure all the class variables it references are also static. Your design choice shouldn't make any different from your users' perspective.

 

(If Gile's answers and mine combined don't answer your question, then I think you need to restate the question, because that would imply we're not understanding your full meaning. Perhaps there's a specific quirk in behavior you've noticed that is causing you to ask this question?).

 

BTW On a related topic, you may not have noticed that we introduced in AutoCAD 2015 a ‘PerDocument’ attribute allows you to register a class so that it is automatically instantiated for every document already open when your app first loads, and for every new document created after that.

Cheers,

Stephen Preston
Autodesk Developer Network
Message 6 of 7

Thanks to both of you for detailed responses! What you are saying matches what I understood about static items.

 

That part about non-static classes being instantiated once per drawing could indeed be useful.

It might check if a menu or whatever is loaded or other things.

 

So I am concluding that Kean's choice was not relevent to the point he was making.

He likely did it because if you are making global vars to all docs, it is likley you would have other items static.

I'll mark both items as solutions, thx

 

 


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

Message 7 of 7

Hi James,

 

I just reread your question and noticed Kean's blog post was written in 2006. If you're trying to implement a custom sysvar (not just a command), then you may prefer to use the API for that we've introduced since then (sorry can't remember which release) - see the Variables and Variable classes in the AutoCAD .NET Reference Guide.

Cheers,

Stephen Preston
Autodesk Developer Network

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