【问题标题】:Using an object from another method [closed]使用来自其他方法的对象 [关闭]
【发布时间】:2013-09-26 00:52:44
【问题描述】:

我的程序是这样的: 我有一个类的公共“主要方法”,它有一个带有值的数据表。 我希望当用户按下特定按钮时,我将使用类(公共类名())的“主方法”中的数据表计算按钮事件。 反正有这样做吗?

【问题讨论】:

  • 绝对!但是,您必须对信息更加坦率。您提供的内容让您很难理解您需要什么。
  • 听起来你可以从阅读一些基本的 c# 教程或 c# 入门书籍中受益。
  • +1 因为你是新手,欢迎堆栈溢出。 Ofir,如果您正在构建 Windows 应用程序、Windows 窗体或 WPF,则不太可能将数据加载到应用程序的 main 方法中。我认为你应该多阅读一些关于 C# 中的 windows 应用程序的信息,通常你只在需要时才加载数据,并将其作为参数或类字段/属性从方法传递到方法。
  • 你们谈论properites和get\set的东西?

标签: c# object methods


【解决方案1】:

这是一个简单的例子:

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());
            ...
        }

【讨论】:

  • 谢谢,如果没有其他人有其他选择,我可以使用它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2020-04-10
相关资源
最近更新 更多