Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calling a Method from a Form event

Calling a Method from a Form event

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ormonds
    wrote on last edited by
    #1

    Can someone please help a newbie with what I suspect is a simple error? Program.cs:

    using System.IO;

    namespace CreateDT
    {
    static class Program
    {
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
    }
    static void MyMethod (name1 string, name2 string)
    {
    // do some stuff with files using the names provided
    }
    }
    }

    Form1.cs:

    using System;
    // other using statements

    namespace CreateDT
    {
    public partial class Form1 : Form
    {
    //variable definitions..
    public Form1()
    {
    InitializeComponent();
    }
    private void DoItButton_Click(object sender, EventArgs e)
    {
    // set names of currentfile, TargetFile
    MyMethod(currentfile, TargetFile);
    }

    Gives error "CS0103 The name 'MyMethod' does not exist in the current context." I've looked at lots of examples which all seem to be done this way. Obviously not, what am I missing?

    L OriginalGriffO 2 Replies Last reply
    0
    • O ormonds

      Can someone please help a newbie with what I suspect is a simple error? Program.cs:

      using System.IO;

      namespace CreateDT
      {
      static class Program
      {
      static void Main()
      {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
      }
      static void MyMethod (name1 string, name2 string)
      {
      // do some stuff with files using the names provided
      }
      }
      }

      Form1.cs:

      using System;
      // other using statements

      namespace CreateDT
      {
      public partial class Form1 : Form
      {
      //variable definitions..
      public Form1()
      {
      InitializeComponent();
      }
      private void DoItButton_Click(object sender, EventArgs e)
      {
      // set names of currentfile, TargetFile
      MyMethod(currentfile, TargetFile);
      }

      Gives error "CS0103 The name 'MyMethod' does not exist in the current context." I've looked at lots of examples which all seem to be done this way. Obviously not, what am I missing?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The method 'MyMethod' is inside (= a member of) the (static) class 'Program', so you have to use

      Program.MyMethod(currentfile, TargetFile);

      1 Reply Last reply
      0
      • O ormonds

        Can someone please help a newbie with what I suspect is a simple error? Program.cs:

        using System.IO;

        namespace CreateDT
        {
        static class Program
        {
        static void Main()
        {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
        }
        static void MyMethod (name1 string, name2 string)
        {
        // do some stuff with files using the names provided
        }
        }
        }

        Form1.cs:

        using System;
        // other using statements

        namespace CreateDT
        {
        public partial class Form1 : Form
        {
        //variable definitions..
        public Form1()
        {
        InitializeComponent();
        }
        private void DoItButton_Click(object sender, EventArgs e)
        {
        // set names of currentfile, TargetFile
        MyMethod(currentfile, TargetFile);
        }

        Gives error "CS0103 The name 'MyMethod' does not exist in the current context." I've looked at lots of examples which all seem to be done this way. Obviously not, what am I missing?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Methods (and fields, properties and events) are part of the class definition: So when you add a method to ClassA, it doesn't exist as part of ClassB. So to access a ClassA method from ClassB, you have to specify which class you are referring to; which class the system should look at - either by specifying a class instance:

        Button b = new Button();
        b.Text = "Click me!";

        Or (in the case of static methods like MyMethod) by specifying the class name:

        Program.MyMethod(currentfile, TargetFile);

        But you shouldn't really be modifying the Program.cs file - instead add a Utilities.cs file, and create a static class in that. To DO that, right click your project name in the Solution Explorer pane, and select "Add"..."Class...". Change the name from "Class1" to "Utilities" and click the "Add" button. Change the class to static and add your method to that. Then when you write your code, it's a bit more obvious that these are designed as "generic helper" methods:

        Utilities.MyMethod(currentfile, TargetFile);

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        O 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Methods (and fields, properties and events) are part of the class definition: So when you add a method to ClassA, it doesn't exist as part of ClassB. So to access a ClassA method from ClassB, you have to specify which class you are referring to; which class the system should look at - either by specifying a class instance:

          Button b = new Button();
          b.Text = "Click me!";

          Or (in the case of static methods like MyMethod) by specifying the class name:

          Program.MyMethod(currentfile, TargetFile);

          But you shouldn't really be modifying the Program.cs file - instead add a Utilities.cs file, and create a static class in that. To DO that, right click your project name in the Solution Explorer pane, and select "Add"..."Class...". Change the name from "Class1" to "Utilities" and click the "Add" button. Change the class to static and add your method to that. Then when you write your code, it's a bit more obvious that these are designed as "generic helper" methods:

          Utilities.MyMethod(currentfile, TargetFile);

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          O Offline
          O Offline
          ormonds
          wrote on last edited by
          #4

          Got it. Thank you.

          OriginalGriffO 1 Reply Last reply
          0
          • O ormonds

            Got it. Thank you.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            You're welcome!

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups