How to make a per-document variable with .net?

On the autocad side, a variable will be global to a document, unless declared as static. Static cars are global to all docs. 

On the bricscad side, it seems a static variable is still global to all docs, but a non-static var is not global to anything.

Use this test code to see (from Kean's blog...):

public class SecondClass {
//if this var is static, it affects all sessions
private int counter1 = 0;
private static int counter2 = 0;

[CommandMethod("loc")]
public void local() {
Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nCounter value is: " + counter1++);
}

[CommandMethod("loc2")]
public void local2() {
Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nCounter value is: " + counter2++);
}
}

When I run the loc command in bcad, its 0 every time.

In acad, it counts up from 0, and starts over in each doc as its nont static.

How would I make a vra global to each doc, not all docs?

Comments

  • meant "Static vars are global to all docs."...
  • [code]

    namespace BrxMgdTestCs
    {
        public class PersistedClass
        {
            public int val;

            public PersistedClass()
            {
                val = 0;
            }
        }
        public class Commands : IExtensionApplication
        {
            public void Initialize()
            {
            }

            public void Terminate()
            {
            }

            [CommandMethod("doit")]
            public static void doit()
            {
                Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
                PersistedClass pclass = new PersistedClass();
                pclass.val = doc.Editor.GetInteger("Enter a number").Value;
                doc.UserData.Add(typeof(PersistedClass), pclass);

            }

            [CommandMethod("getit")]
            public static void getit()
            {
                Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
                if(doc.UserData.ContainsKey(typeof(PersistedClass)))
                {
                    PersistedClass pclass = doc.UserData[typeof(PersistedClass)] as PersistedClass;
                    doc.Editor.WriteMessage(pclass.val.ToString());
                }
            }
        }
    }
     [/code]

  • namespace BrxMgdTestCs
    {
        public class PersistedClass
        {
            public int val;

            public PersistedClass()
            {
                val = 0;
            }
        }
        public class Commands : IExtensionApplication
        {
            public void Initialize()
            {
            }

            public void Terminate()
            {
            }

            [CommandMethod("doit")]
            public static void doit()
            {
                Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
                PersistedClass pclass = new PersistedClass();
                pclass.val = doc.Editor.GetInteger("Enter a number").Value;
                doc.UserData.Add(typeof(PersistedClass), pclass);
            }

            [CommandMethod("getit")]
            public static void getit()
            {
                Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
                if(doc.UserData.ContainsKey(typeof(PersistedClass)))
                {
                    PersistedClass pclass = doc.UserData[typeof(PersistedClass)] as PersistedClass;
                    doc.Editor.WriteMessage(pclass.val.ToString());
                }
            }
        }
    }

  • Of course you can use any type for the key , object.  It could just as well be:
    doc.UserData.Add(String, int);
    The plus side of using Document.UserData, is that it easy to get the values for any document  from any command and or class

    UserData is a HashTable

    Cheers

  •  


    The ubiquitous DOIT command strikes again :)

    Best wishes for a safe holiday guys.

    Regards,

  •  


    The ubiquitous DOIT command strikes again :)

    Best wishes for a safe holiday guys.

    Regards,

    Lol, I wonder how many doit functions I have out there : )

    Best wishes to you and family as well : )

  • more fun

    namespace BrxMgdTestCs
    {
        public class PersistedClass
        {
            public int val;
            public PersistedClass()
            {
                val = 0;
            }
            public void DoSomething()
            {
                Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
                doc.Editor.WriteMessage(val.ToString());
            }
        }
        public class Commands
        {
            [CommandMethod("doit")]
            public static void doit()
            {
                PersistedClass pclass = null;
                Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
                if (doc.UserData.ContainsKey(typeof(PersistedClass)))
                {
                    pclass = doc.UserData[typeof(PersistedClass)] as PersistedClass;
                }
                else
                {
                    pclass = new PersistedClass();
                    doc.UserData.Add(typeof(PersistedClass), pclass);
                }
                if (pclass != null)
                {
                    pclass.val++;
                    pclass.DoSomething();
                }
            }
        }
    }

  • so doc.userdata is the only way? Its funny because is the acad behavior of non-static vars correct, or bcad?

    It does not matter, I have to choose a method that works for both as I want one codeset with as few #if forks as possible.

    Thanks for the responses - with example code no less, much appreciarted.

This discussion has been closed.