[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Multiple classes to a single declaration in form

Hello experts:
I'm doing an applicaton that has a main form, configuration form, configuration class and two (for lack of better terms) main classes. The main form starts. Before it is displayed, the configuration form displays as a dialog form. It is the interface to the configuration class. When the configuration form is terminated with 'OK', the main form displays. Based on the configuration information, the main form must have a reference to either main1 or main2 class. The main form will reference either of these two classes as main(not referring to main method() ). How do I do this?
0
freename
Asked:
freename
1 Solution
 
nguyenvinhtuCommented:
OK, the main idea here is
- Create MainForm first, but user cannot interact with it.
- Create ConfigDialogForm later with button OK.
- When user click button OK on ConfigDialogForm, we will return a string with value "MainAClass" or "MainBClass" to MainForm object.
----------The question is how we can return the string to MainForm object?
----------That is we will pass the MainForm object into Constructor of ConfigFormDialog (see how to do below)
- After that, make a condition statement to decide which class, MainAClass or MainBClass should be instantiated.
----------We have the same object is MainClass, but how can we have 2 different ways of functioning. We use polymophism in .NET.
----------Create Abstract class named MainClass, and ... see how below.
So, you have 6 class.
- class MainForm
- class ConfigDialogForm
- class ConfigClass
- class MainClass (abstract class)
- class MainAClass
- class MainBClass.
OK?
If so, let move on.
//MainForm Constructor
public class MainForm
{
      public string strChooseWhichMainClass;
      public MainClass a;
      public MainForm()
      {
              InitializeComponent();

              //Make a call to ConfigDialogForm right down here.
              ConfigDialogForm confForm = new ConfigDialogForm(this); //We need a reference to MainForm
              confForm.Visisble = true; //It means you also open ConfigDialogForm

              //You may want to disable all function of MainForm here.
              //If users click Cancel on ConfigDialogForm, it means they cannot use the application.
        }


        public void btnProcessMainFormFunction_Click(object sender, System.EventArgs e)
        {
              if(strChooseWhichMainClass != "") //To make sure user click button OK now, if not, nothing will happen when click this button.
              {
                   if(strChooseWhichMainClass.Equals("MainAClass"))
                   {
                         a = new MainAClass();
                   }
                   else
                   {
                         a = new MainBClass();
                   }
              }
        }
}

So, in the class ConfigDialogForm, you will create object ConfigClass which has some functions to Process request from user.
public class ConfigDialogForm
{
       public MainForm MForm1;

       //user defined constructor
       public ConfigDialogForm(MainForm form1)
       {
               //After this line of code, you now have an object of MainForm in ConfigDialogForm,
                MForm1 = form1;
       }

       public void btnOK_Click(object sender, System.EventArgs e)
      {
               //After this line of code, variable named strChooseWhichMainClass will have result as "MainAClass" or MainBClass
               MForm1.strChooseWhichMainClass = RequestWhichMainClass(<params>);

      }

       public string RequestWhichMainClass(<params>)
       {
             ConfigClass confgObj = new ConfigClass();
             return confgObj.ChooseWhichMainClass(<params>);
       }
}

public abstract MainClass
{
        function1();
        function2();
        function3();
}

public class MainAClass : MainClass
{
       //Overloading function1,2,3 above in MainAClass way.
}

public class MainBClass : MainClass
{
       //Overloading function1,2,3 above in MainBClass way.
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now