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;
}
}
}
QuoteMgrclass, would help.