How do I defile a global variable in C#? Thanks.
|
"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
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
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
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
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(); } } }
It must be contained within a class. You can create static or shared within a project but to accesss it you must use the class like MyClass.MyVariable.
VB will do things under the hood to mimic global varibles. If you place your variable in a module behind the scences VB adds a imports statement to each file in the project since in VB you can import elements(classes, modules, struct, enums, ....) so it seems you are not accessing the variable through the class but MyVariable will be replaced with MClass.MyVarible when compiled.
Jeff, as always, thanks for the clarification.
This is something that I have done before, despite my earlier confusion; recently in the context of a shared function that resided in my "__.vb" Class which contains many sub-functions, so that calls to this function from other Classes resembled core AutoCAD command line calls such as "__.MySubFunc()". *geek*
I am really trying to give C# a go - I've already purchased Andrew Treolsen's "Pro C# 2010 and the .NET 4 Platform" at Kerry's recommendation.
"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