Calling a Method from a Form event
-
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 statementsnamespace 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?
-
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 statementsnamespace 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?
-
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 statementsnamespace 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?
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!
-
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!
-
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!