Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: C# Global Variable

  1. #1
    All AUGI, all the time
    Join Date
    2007-09
    Location
    Raleigh NC
    Posts
    755
    Login to Give a bone
    0

    Default C# Global Variable

    How do I defile a global variable in C#? Thanks.

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: C# Global Variable

    Quote Originally Posted by bryan.thatcher View Post
    How do I define a global variable in C#? Thanks.
    FTFY.

    Is this what you're after?

    Quote Originally Posted by AutoCAD .NET Developer's Guide: Register COM Based Events with .NET

    <snip>

    Code:
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Interop;
    using Autodesk.AutoCAD.Interop.Common;
      
    // Global variable for AddCOMEvent and RemoveCOMEvent commands
    AcadApplication acAppCom;
    
    [CommandMethod("AddCOMEvent")]
    public void AddCOMEvent()
    {
      // Set the global variable to hold a reference to the application and
      // register the BeginFileDrop COM event
      acAppCom = Application.AcadApplication as AcadApplication;
      acAppCom.BeginFileDrop += 
    
         new _DAcadApplicationEvents_BeginFileDropEventHandler(appComBeginFileDrop);
    }
      
    [CommandMethod("RemoveCOMEvent")]
    public void RemoveCOMEvent()
    {
      // Unregister the COM event handle
      acAppCom.BeginFileDrop -= 
          new _DAcadApplicationEvents_BeginFileDropEventHandler(appComBeginFileDrop);
      acAppCom = null;
    }
      
    public void appComBeginFileDrop(string strFileName, ref bool bCancel)
    {
      // Display a message box prompting to continue inserting the DWG file
      if (System.Windows.Forms.MessageBox.Show("AutoCAD is about to load " + strFileName +
                                           "\nDo you want to continue loading this file?",
                                           "DWG File Dropped",
                                           System.Windows.Forms.MessageBoxButtons.YesNo) == 
                                           System.Windows.Forms.DialogResult.No)
      {
          bCancel = true;
      }
    }
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    Active Member
    Join Date
    2009-08
    Posts
    93
    Login to Give a bone
    0

    Default Re: C# Global Variable

    Quote Originally Posted by bryan.thatcher View Post
    How do I defile a global variable in C#? Thanks.
    You don't it is currently impossible in C#.
    Everthing must be defined within a type definition.
    You can do it in CIL though.

  4. #4
    Active Member
    Join Date
    2006-08
    Location
    Brisbane : GMT+10
    Posts
    87
    Login to Give a bone
    0

    Default Re: C# Global Variable

    Quote Originally Posted by bryan.thatcher View Post
    How do I defile a global variable in C#? Thanks.
    What is your definition of 'Global'

    ie : how long to you want to keep the value
    and what do you want to access it from,
    and will it be local to a document or to the application ?

    You could save it to the Lisp environment, save it to file or the registry, or a couple of other methods, depending on the answers.
    As Jeff said, C# does not have a globally accessable memory block that can be accessed from disparate programs/assemblies.

    Regards

  5. #5
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: C# Global Variable

    Kerry / Jeff -

    I did not know this about C#, does VB.NET also suffer this shortcoming? I thought that was the point of using a Namespace within an Assembly, to allow for defining of variables, Classes, Properties, etc. without the risk of creating a conflict between Namespaces, no?
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  6. #6
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,396
    Login to Give a bone
    0

    Default Re: C# Global Variable

    I think the clue is in Kerry's last statement:
    Quote Originally Posted by Kerry.Brown View Post
    C# does not have a globally accessable memory block that can be accessed from disparate programs/assemblies.
    The user was probably using the term as its used in vb, i.e. it has scope to the whole project/namespace.
    C:> ED WORKING....

  7. #7
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: C# Global Variable

    Thanks for the hint Ed, unfortunately for me though, most of what Kerry, the Jeff's (M & H), Gile, Dgorsman (and the like) post is usually above my ahead. Don't even get me started on TheMaster (Mr. T)! LoL
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  8. #8
    All AUGI, all the time
    Join Date
    2007-09
    Location
    Raleigh NC
    Posts
    755
    Login to Give a bone
    0

    Default Re: C# Global Variable

    I'm trying to clean up the following code. In fact, there may be an easier way to accomplish what i'm doing, but this is where i'm at. I would like to define the categories once and reference them. One problem is they are dependent on the 'commandData'. Thoughts? Thanks.

    Code:
    namespace Projects.QuickVisibility_4.CS
    {
        public partial class VisibilityForm : System.Windows.Forms.Form
        {
            UIDocument docUI;
            Document doc;
    
    
            //Check category visibility in the view and populate the checkbox
            public VisibilityForm(ExternalCommandData commandData)
            {
                docUI = commandData.Application.ActiveUIDocument;
                doc = commandData.Application.ActiveUIDocument.Document;
    
                RvtView curView = doc.ActiveView;
    
                Category catSections = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Sections);
                Category catGrids = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Grids);
                Category catRefplanes = doc.Settings.Categories.get_Item(BuiltInCategory.OST_CLines);
    
                InitializeComponent();
    
                bool sectionVis = curView.getVisibility(catSections);
                checkBox1.Checked = sectionVis == true ? true : false;
    
                bool gridVis = curView.getVisibility(catGrids);
                checkBox2.Checked = gridVis == true ? true : false;
    
                bool refplaneVis = curView.getVisibility(catRefplanes);
                checkBox3.Checked = refplaneVis == true ? true : false;
            }
    
            //Get checkbox value and set visibilty in the view
            private void button1_Click(object sender, EventArgs e)
            {
                RvtView curView = doc.ActiveView;
    
                Category catSections = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Sections);
                Category catGrids = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Grids);
                Category catRefplanes = doc.Settings.Categories.get_Item(BuiltInCategory.OST_CLines);
    
                Transaction trans = new Transaction(doc);
                trans.Start("Hide or Unhide");
    
                bool sTorF = checkBox1.Checked == true ? true : false;
                curView.setVisibility(catSections, sTorF);
    
                bool gTorF = checkBox2.Checked == true ? true : false;
                curView.setVisibility(catGrids, gTorF);
    
                bool rTorF = checkBox3.Checked == true ? true : false;
                curView.setVisibility(catRefplanes, rTorF);
    
                trans.Commit();
            }
        }
    }

  9. #9
    Active Member
    Join Date
    2006-08
    Location
    Brisbane : GMT+10
    Posts
    87
    Login to Give a bone
    0

    Default Re: C# Global Variable

    Bryan,
    Is what you want to do different to the way you've declared and initialise the variables docUi and doc ??

    BTW.
    Naming the method the same name as the class is not usual practice.
    Usually a class is a noun, a Method is a verb
    ref: http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

  10. #10
    All AUGI, all the time
    Join Date
    2007-09
    Location
    Raleigh NC
    Posts
    755
    Login to Give a bone
    0

    Default Re: C# Global Variable

    Yes, doing the variables docUi and doc differently would be nice also, what are my options? But really I was trying to not have to define categories two times.

Page 1 of 2 12 LastLast

Similar Threads

  1. Grid Scale global variable
    By Wish List System in forum Civil 3D Wish List
    Replies: 0
    Last Post: 2014-09-17, 02:17 PM
  2. Global Variable
    By zoommby in forum Revit - Platform
    Replies: 1
    Last Post: 2012-01-19, 09:14 PM
  3. Global variable/parameter/attribute library?
    By trailbarge in forum AutoCAD General
    Replies: 4
    Last Post: 2009-08-31, 06:49 PM
  4. Variable height/variable count family (studs in a sloped wall)
    By DoTheBIM in forum Revit Architecture - Families
    Replies: 6
    Last Post: 2006-06-15, 02:48 PM
  5. Global scale variable in metric and imperial
    By rolibolibo in forum AutoLISP
    Replies: 2
    Last Post: 2006-02-24, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •