Graympa
Graympa

Reputation: 9

Using methods from another class in forms, C#

Thank you very much for the response. I have edited my post to show the QuoteMgr class. I use this mostly to read and save quotes to file; it is read back into an array of quotes called mylist. I can't figure out how to call QuoteMgr methods from within all of the four forms I have created. The only way I have found is to instantiate QuoteMgr from within one of the forms, but that won't work for the other three forms. The method I want to use in different forms is getRandomQuote() - haven't written the other methods yet. My plan was to read data from a file, display the quote on the main form, and offer choice to add more quotes, edit one, or display another quote. A different form would be displayed, appropriate to the choice made.

At heart the problem is I don't fully grasp OOP. I understand the idea of having an abstract class to inherit methods from. If I do this, will the different forms be able to access my array of quotes ("mylist")? For data integrity, I think I only want one instance of my data floating around. In which case, I could have an abstract class with all of the quote manipulation methods, and use QuoteMgr only to read/write to file.

From the standpoint of learning the right way to program, is this the right design?

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

namespace Quote { class 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;
        }

    }
}

Upvotes: 0

Views: 1246

Answers (1)

alexD
alexD

Reputation: 2364

It sounds like you should move the changeQuoteDisplayed method into your Form class...it doesn't make any sense to call a method in another class with your Form as an argument, only to have the other class modify the form you passed it. For one, you should not have public UI components inside your Form..if you must modify these from outside the form, make their data accessible through Properties.

If this is a method that all of your forms need to use, then perhaps you should allow them to inherit it through an abstract class, providing an abstract property as well that your child classes will use to implement set, making it update whatever UI component the child class needs updated on that method call. It could look something like this:

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; }
        }
  }

Now all you need to do is implement the LabelText property in all of your Quote classes to update whatever label you want without needing to send an instance of your form to some other class to get an update.

Of course, this is just a wild guess..it's hard to tell what you should actually do without more information.

Upvotes: 2

Related Questions