Results 1 to 3 of 3

Thread: The name 'myFirstFunction' does not exist in the current context

  1. #1
    Active Member
    Join Date
    2008-02
    Location
    Toronto, On
    Posts
    59
    Login to Give a bone
    0

    Default The name 'myFirstFunction' does not exist in the current context

    Hey all,

    I'm trying to get started with .net. I've worked through the DevTV labs and got a bunch of great info, now I'm trying to make some magic happen, and I'm running into a bit of trouble.

    Through the DevTV labs, you create a userControl, and make a command which uses the usercontrol as a class to create a paletteSet, and display it. The palette that you create has a treeView on it which gets populated with entity info, and later you create a drag-and-drop event from a label on the palette.

    This looked like a pretty good starting point for something I'd like to do. I want to make a paletteSet of some common commands for my users. I have plans of having controls related to those commands next to buttons which will launch the commands. For now, I've added some buttons to my userForm, which I want to trigger commands.

    Code:
    ...using...
    
    namespace FTools
    {
        public class AutoCADTools
        {
            public PaletteSet myPaletteSet;
    
            public FTools.FToolsPalette myPalette;
            
    
            [CommandMethod("FTools")]
            public void showFToolsPalette()
            {
                if (myPaletteSet == null)
                {
                    myPaletteSet = new PaletteSet("FTools", new System.Guid("FDA43192-4967-453B-8EB4-6D9CDEFA2A33"));
    
                    myPalette = new FTools.FToolsPalette();
    
                    myPaletteSet.Add("FTools", myPalette);
                }
                
                myPaletteSet.Visible = true;
            }
    
    
            public void myFirstFunction()
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    
                ed.WriteMessage("I would now run myFirstFunction");
    
            }
    
    
            public void mySecondFunction()
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    
                ed.WriteMessage("I would now run mySecondFunction");
    
            }
    
    
    
        }
    }
    This compiles, and when I netLoad and run FTOOLS, my paletteSet is displayed, and I love it.

    So here's the code of the FToolsPalette class:

    Code:
    ...using...
    
    namespace FTools
    {
        public partial class FToolsPalette : UserControl
        {
            public FToolsPalette()
            {
                InitializeComponent();
            }
    
            private void FirstFunctionButton_Click(object sender, EventArgs e)
            {
                myFirstFunction();
    
            }
        }
    }
    And the line " myFirstFunction() " is highlighted with the message "The name 'myFirstFunction' does not exist in the current context"

    I think this has to do with the fact that the FToolsPalette exists in the context of the window(?) and the function would need to be run in the context of the editor(?) but I'm new here and I'm not sure if that is correct.

    Does the function which a button calls need to be in the same class as the button? How does this allow me to organize my code into separate classes?

    How far off the mark am I?

    Thanks!

    EDIT:
    I also put a comboBox onto the palette in visual studio. How do I access this comboBox?

    I thought that after creating a new FToolsPalette called myPalette, I could do myPalette.comboBox1, but myPalette doesn't intellisense anything after it...
    Last edited by JaCAD; 2014-03-20 at 03:09 PM.

  2. #2
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    201
    Login to Give a bone
    0

    Default Re: The name 'myFirstFunction' does not exist in the current context

    Hi,

    As your code is, myFirstFunction is an instance method of the AutoCADTools class.
    To call an instance method, you have to create an instance of the class and call the method from this instance this way
    Code:
    AutoCADTools tool = new AutoCADTools();
    tool.myfirstFunction();
    But this is not really the way this may be done (no interest to create an instance of the AutoCADTools class).

    I see two better ways to solve this.

    You can define the myFirstFunction() method in the FToolsPalette class.

    You can also let myFirstFunction() method in the AutoCADTools class and declare it static so that you can call it from everywhere using the syntax:
    Code:
    public static void myFirstFunction()
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage("I would now run myFirstFunction");
            }
    from FToolsPalette class (or elsewhere):
    Code:
    AutoCADTools.myFirstFunction();
    Last edited by _gile; 2014-05-09 at 04:35 PM.

  3. #3
    Active Member
    Join Date
    2008-02
    Location
    Toronto, On
    Posts
    59
    Login to Give a bone
    0

    Default Re: The name 'myFirstFunction' does not exist in the current context

    Quote Originally Posted by _gile View Post
    Hi,

    As your code is, myFirstFunction is an instance method of the AutoCADTools class.
    To call an instance method, you have to create an instance of the class and call the method from this instance this way
    Code:
    AutoCADTools.tool = new AutoCADTools();
    toll.myfirstFunction();
    But this is not really the way this may be done (no interest to create an instance of the AutoCADTools class).

    I see two better ways to solve this.

    You can define the myFirstFunction() method in the FToolsPalette class.

    You can also let myFirstFunction() method in the AutoCADTools class and declare it static so that you can call it from everywhere using the syntax:
    Code:
    public static void myFirstFunction()
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage("I would now run myFirstFunction");
            }
    from FToolsPalette class (or elsewhere):
    Code:
    AutoCADTools.myFirstFunction();
    Thanks gile, this is where I went with it and it worked.

Similar Threads

  1. commandData does not exist in the current context
    By scowsert in forum Revit - API
    Replies: 4
    Last Post: 2009-07-28, 05:59 PM
  2. Keep current Layer Filter current while opening and closing
    By kathy71046 in forum AutoCAD General
    Replies: 8
    Last Post: 2007-05-24, 02:35 AM
  3. Context sensitive help
    By tomm in forum Revit Architecture - General
    Replies: 3
    Last Post: 2004-12-01, 10:01 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
  •