使用表单中另一个类的方法,C#

时间:2011-06-02 05:38:25

标签: c# methods call

非常感谢您的回复。我编辑了我的帖子以显示QuoteMgr类。我主要用它来读取并保存引号到文件;它被读回一个名为mylist的引号数组。我无法弄清楚如何从我创建的所有四种形式中调用QuoteMgr方法。我找到的唯一方法是从其中一个表单中实例化QuoteMgr,但这对其他三个表单不起作用。我想以不同形式使用的方法是getRandomQuote() - 尚未编写其他方法。 我的计划是从文件中读取数据,在主窗体上显示引号,并提供选择以添加更多引号,编辑引号或显示另一个引号。将显示一个适合所做选择的不同形式。

问题在于我没有完全掌握OOP。我理解有一个抽象类来继承方法的想法。如果我这样做,不同的表单是否能够访问我的引号数组(“mylist”)?对于数据完整性,我想我只希望我的数据的一个实例浮动。在这种情况下,我可以有一个带有所有引用操作方法的抽象类,并且只使用QuoteMgr来读/写文件。

从学习正确的编程方式来看,这是正确的设计吗?

使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用System.IO; 使用System.Runtime.Serialization.Formatters; 使用System.Runtime.Serialization.Formatters.Binary; 使用System.Windows.Forms;

命名空间引用     {     类QuoteMgr         {

    Quotes myquote = new Quotes();
    Quotes objectToSerialize = new Quotes();
    Serializer serializer = new Serializer();
    string myFile = "H:\\Dad\\Quotes\\quotes.quo";


    public QuoteMgr()
        {
        }

    static Random r = new Random();

    public void getFile()
        {
        //fills myquote.mylist with quote strings from file

        if (File.Exists(myFile))
            {
            objectToSerialize = serializer.DeSerializeObject(myFile);
            myquote.myList = objectToSerialize.QuoteList;
            //myquote.ShowQuotes();
            }
        else
            {
            FileInfo makeFile = new FileInfo(@myFile);
            makeFile.Create();
            }

        }//end of get file

    public void saveFile()
        {
        objectToSerialize.QuoteList = myquote.myList;
        serializer.SerializeObject(myFile, objectToSerialize);
        }

    public string getRandomQuote()
        {
        int x = myquote.myList.Count-1;
        return myquote.myList[r.Next(x)];
        }

    public void GUIop()
        {
        getFile();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 myMainScreen = new Form1();


        //make all other forms invisible
        myMainScreen.Visible = true;


        changeQuoteDisplayed(myMainScreen);
        Application.Run(myMainScreen);
        saveFile();
        }

    public void changeQuoteDisplayed(Form1 theForm) 
        {
        string s;
        s = getRandomQuote();
        theForm.lblDisplayQuote.Text = s;
        }

    }
}

1 个答案:

答案 0 :(得分:2)

听起来你应该将changeQuoteDisplayed方法移动到Form类中...将Form作为参数调用另一个类中的方法没有任何意义,只有另一个类才有意义修改您传递的表单。例如,您不应在表单中包含公共UI组件。如果必须从表单外部修改这些组件,请通过“属性”访问其数据。

如果这是一个所有表单需要使用的方法,那么也许您应该允许它们通过抽象类继承它,提供一个抽象属性以及您的子类将用于实现set ,使它更新子类需要在该方法调用上更新的任何UI组件。它可能看起来像这样:

public abstract class QuoteBase
{
         protected void changeQuoteDisplayed() 
         {
             string s;
             s = getRandomQuote();
             LabelText = s;
            // theForm.lblDisplayQuote.Text = s;
          }

          public abstract String LabelText
          {
                   get; set;
          }
 }

 public class EditQuote : QuoteBase
 {
        public override String LabelText
        {
               get { return lblDisplayQuote.Text; }
               set { lblDisplayQuote.Text = value; }
        }
  }

现在您需要做的就是在所有Quote类中实现LabelText属性,以更新您想要的任何标签,而无需将表单实例发送到其他类来获取更新。

当然,这只是一个疯狂的猜测......如果没有更多信息,很难说出你应该做些什么。