-3

我想知道如何在程序期间保存用户输入。

我的代码:

struct client
{
    public string points;//to collect the points from a textbox

    public int  x;//variable used to temporarily save the input from the textbox
    public int will;//used to store his points until the end of the program
    public int steph;//used to store here points until the end of the program
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
      client ob = new client();//instance of client
      ob.points = textBox1.Text;             
      ob.x  = Convert.ToInt32(ob.points);//conversion
      MessageBox.Show(ob.x.ToString());//used to confirm points
      button1.Enabled = true;//enables button to show where the points should go
      button2.Enabled = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        client ob = new client();
        ob.points = textBox1.Text;           
        ob.x = Convert.ToInt32(ob.points);
        ob.will = ob.will + ob.x;
        MessageBox.Show(ob.will.ToString());//actually adding the points to him
    }

    private void button2_Click(object sender, EventArgs e) 
    {
        client ob = new client();
        ob.points = textBox1.Text;
        ob.x = Convert.ToInt32(ob.points);
        ob.steph = ob.steph + ob.x;// actually adding the points to her
        MessageBox.Show(ob.steph.ToString());
    }
}

有人告诉我我需要添加我的客户的三个实例,但这就是他们给我的所有信息。当您告诉我时,请您深入回答。另外,如果有办法使代码更高效,请告诉我。

4

2 回答 2

2

你的问题是

client ob = new client();//instance of client

这一行每次都会创建一个新的客户端实例,您正在寻找的是单例类

public sealed class Client
{
   private static readonly Client instance = new Client();

   private Client(){}

   public static Client Instance
   {
      get 
      {
         return instance; 
      }
   }
   public int  X {get;set;}
}

然后每当你需要你的客户时,你打电话

Client.Instance
Clienet.Instance.X

等等..

有关更多信息,请参阅

于 2013-08-04T20:19:12.307 回答
0

如果我理解了这个问题,我认为您可以使用全局类变量。它可以是:
List<Client> clients并且您将在每个按钮单击新客户端后添加到全局List<Client>

于 2013-08-04T20:15:48.693 回答