C# |
Code |
Error 1 An object reference is required for the non-static field, method, or property
C# |
Code |
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
FroglegRank #1,020 · 1.9K reputationPosted Feb 5, 2011, 8:45 AM EST
namespace StaticMethod
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public static void Method1()
{
Form1 Fm1Instance = new Form1();// need instance
Fm1Instance.Method2();
}
private void Method2()
{
MessageBox.Show("Method2");
}
public static void Method3()
{
Method4();// can call static
}
public static void Method4()
{
MessageBox.Show("Method4");
}
private void bttnShowForm2_Click(object sender, EventArgs e)
{
Form2 fm2 = new Form2();
fm2.Show();
}
private void bttnmethod1_Click(object sender, EventArgs e)
{
Method1();
}
private void bttnMethod4_Click(object sender, EventArgs e)
{
Method3();
}
}
}
Form2-------------
namespace StaticMethod
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void bttnMethod1_Click(object sender, EventArgs e)
{
Form1.Method1();
}
}
}
Sam HobbsRank #107 · 20.5K reputationPosted Feb 5, 2011, 9:26 AM EST