C# 使用另一方法中的对象

C# 使用另一方法中的对象,c#,object,methods,C#,Object,Methods,我的程序是这样的: 我有一个类的公共“main方法”,它得到一个带有值的数据表。 我希望当用户按下一个特定的按钮时,我将使用该类的“main方法”(public classname())中的数据表计算按钮事件中的某些内容。 有什么办法可以做到这一点 下面是一个简单的例子: using System.Data; ... namespace WindowsFormsApplication1 { public partial class Form1 : Form {

我的程序是这样的: 我有一个类的公共“main方法”,它得到一个带有值的数据表。 我希望当用户按下一个特定的按钮时,我将使用该类的“main方法”(public classname())中的数据表计算按钮事件中的某些内容。
有什么办法可以做到这一点

下面是一个简单的例子:

using System.Data;
...
namespace WindowsFormsApplication1
{


    public partial class Form1 : Form
    {
        DataTable dt;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            dt = new DataTable();
            //load your table here
            ...
        }
        // Create an event (maybe via the designer) for button click...
        private void button1_Click(object sender, EventArgs e)
        {
            //you can reference the datatable here, for example tell how many rows it has
            MessageBox.Show(dt.Rows.Count.ToString());
            ...
        }

绝对地不过,你必须对这些信息更加坦率。您提供的内容使您很难理解您需要什么。听起来您可以通过阅读一些基本的c#教程或c#+1入门书获益。因为您是新手,欢迎使用stack overflow。如果您正在构建windows应用程序、windows窗体或WPF,则不太可能在应用程序的主方法中加载数据。我想你应该多读一点关于C#中windows应用程序的内容,通常你只在需要时加载数据,并将其作为参数或类字段/属性从一个方法传递到另一个方法。你们讨论了属性和获取\设置的内容吗?谢谢,如果没有其他人有其他选择,我可能会使用它:)