using System;
namespace myNameSpace
{
class Program
{
private void method()
{
Console.WriteLine("Hello World!");
}
static void Main(string[] args)
{
method();//<-- Compile Time error because an instantiation of the Program class doesnt exist
Program p = new Program();
p.method();//Now it works. (You could also make method() static to get it to work)
}
}
}
static class Program
{
public static Form1 form1; // = new Form1(); // Place this var out of the constructor
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(form1 = new Form1());
}
}
class Form1
{
public static Form1 It; // Singleton.
public Form1()
{
It = this;
}
private static void SumData(object state)
{
...
It.setTextboxText(result);
}
}
点2
private static void SumData(object state){ int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } Form1 frm1 = new Form1(); frm1.setTextboxText(result);}
点3
p4
private void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } setTextboxText(result); }