tags:

views:

43

answers:

2

I'm not that good in OOP or even C# but I want to try to keep my code as clean as possible. Lets say we've got something like that within the Namespace "GoogleCalendarNotificator":

   public partial class MainForm : Form
   {
        private object calendarData;

        public MainForm()
        {
            InitializeComponent();
        }

        private void recieveCalendarDataButton_Click(object sender, EventArgs e)
        {
            getCalendarDataBW.RunWorkerAsync();
        }

        private void getCalendarDataBW_DoWork(object sender, DoWorkEventArgs e)
        {
            try {
                getCalendarData getCalendarDataObj = new getCalendarData();
                calendarData = getCalendarDataObj.getData();
            } catch (Exception err) {
                statusHandler("Login Failed, Please check your Data", err.Message);
            }

            try {
                workWithCalendarData workWithCalendarDataObj = new workWithCalendarData();
                workWithCalendarDataObj.workWithData(calendarData, MainForm.ActiveForm);

                statusHandler("New calendardata recieved");
            } catch (Exception err) {
                statusHandler("Login Failed, Please check your Data", err.Message);
            }
        }

        public void statusHandler(string displayMessage, string tooltipMessage = null)
        {
            string curTime = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
            statusLabel.Text = curTime + " - " + displayMessage;

            if (tooltipMessage != null)
            {
                statusLabel.ToolTipText = "Original error message: " + tooltipMessage;
            }
        }

Is this a good solution for ErrorHandling? And how about this part:

       workWithCalendarData workWithCalendarDataObj = new workWithCalendarData();
       workWithCalendarDataObj.workWithData(calendarData, MainForm.ActiveForm);

I simply want to give another class (workWithCalendarData) the possibility to interact with the MainForm. But I do not want simply written in the workWithCalendarData class something like:

MainForm hehe = new MainForm();
hehe.DOSOMETHING();

I want it to be dynamicly if anybody knows what I mean. Like give the class the Information needed to work with the Form (a.e. MainForm.ActiveForm).

+1  A: 
MainForm hehe = new MainForm();
hehe.DOSOMETHING();

Well, that just doesn't work. It creates a new form, one that isn't visible because Show() wasn't called. You'll need a reference to the existing form, this in the MainForm's code. You can pass it to the worker class by calling its constructor, passing the reference.

That is however a Bad Idea. It makes your worker class dependent on the user interface. Changing the GUI, happens often because it is, well, so visible, will break your worker class. You solve that problem by using events. Let the worker class raise the event when something worthwhile happens. So that the form can then obtain the information from the class object and update the UI accordingly.

Also look into the MVC pattern to get some ideas.

Hans Passant
Great, MVC pattern it is called :) That helps me the most.. Ill continue study more infos like that?
James Hyx
A: 

Remember that your form is also a class. Use standard OOP mechanisms for allowing one class to interact with another. Events, Properties, and Methods can all be used.

John Saunders