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

Thread: C# Global Variable

  1. #1
    All AUGI, all the time Bryan Thatcher's Avatar
    Join Date
    2007-09
    Location
    Raleigh NC
    Posts
    755
    0 Did you find this post helpful? Yes

    Default C# Global Variable

    How do I defile a global variable in C#? Thanks.
    Bryan Thatcher
    Structural Design Technician
    Clark-Nexsen Architecture & Engineering

  2. #2
    Programming Moderator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    3,404
    0 Did you find this post helpful? Yes

    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;
      }
    }
    "Potential has a shelf life." - Margaret Atwood

    Autodesk Exchange Apps
    ~ Autoloader ~ AutoCAD Security

  3. #3
    Active Member
    Join Date
    2009-08
    Posts
    90
    0 Did you find this post helpful? Yes

    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.
    You can also find your answers @ Theswamp

  4. #4
    Active Member Kerry Brown's Avatar
    Join Date
    2006-08
    Location
    Brisbane : GMT+10
    Posts
    87
    0 Did you find this post helpful? Yes

    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
    another Swamper

  5. #5
    Programming Moderator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    3,404
    0 Did you find this post helpful? Yes

    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?
    "Potential has a shelf life." - Margaret Atwood

    Autodesk Exchange Apps
    ~ Autoloader ~ AutoCAD Security

  6. #6
    Forum Manager, Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    4,906
    0 Did you find this post helpful? Yes

    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
    Programming Moderator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    3,404
    0 Did you find this post helpful? Yes

    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
    "Potential has a shelf life." - Margaret Atwood

    Autodesk Exchange Apps
    ~ Autoloader ~ AutoCAD Security

  8. #8
    All AUGI, all the time Bryan Thatcher's Avatar
    Join Date
    2007-09
    Location
    Raleigh NC
    Posts
    755
    0 Did you find this post helpful? Yes

    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();
            }
        }
    }
    Bryan Thatcher
    Structural Design Technician
    Clark-Nexsen Architecture & Engineering

  9. #9
    Active Member Kerry Brown's Avatar
    Join Date
    2006-08
    Location
    Brisbane : GMT+10
    Posts
    87
    0 Did you find this post helpful? Yes

    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
    another Swamper

  10. #10
    All AUGI, all the time Bryan Thatcher's Avatar
    Join Date
    2007-09
    Location
    Raleigh NC
    Posts
    755
    0 Did you find this post helpful? Yes

    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.
    Bryan Thatcher
    Structural Design Technician
    Clark-Nexsen Architecture & Engineering

Page 1 of 2 12 LastLast

Similar Threads

  1. Global Variable
    By zoommby in forum Revit - Platform
    Replies: 1
    Last Post: 2012-01-19, 08:14 PM
  2. Global variable/parameter/attribute library?
    By trailbarge in forum AutoCAD General
    Replies: 4
    Last Post: 2009-08-31, 06:49 PM
  3. Global Code
    By wpeacock in forum VBA/COM Interop
    Replies: 1
    Last Post: 2006-10-20, 02:22 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, 03: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
  •