Link to home
Start Free TrialLog in
Avatar of San24
San24

asked on

Passing forms as a Parameter

Experts,

I`m using C# .NET VS 2008. I was wondering if passing Forms/UserControls as Paramaters to the constructors of Classes is a good practice.

Lets say, I have a Class called ExpertsClass and I want to access the controls of the main form.

Can I do something like this?
//Form1.cs     
MC = new ExpertsClass(this);

//ExpertsClass.cs
//Constructor
public ExpertsClass(Form1 F)
{

}

Open in new window

SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of San24
San24

ASKER

James..If it`s not too much to ask, can you provide an example.

Something like this? See code.

/Form1.cs     
MC = new ExpertsClass(this);

//ExpertsClass.cs
//Constructor

public Form1 Fs = null;     //Property

public ExpertsClass(Form1 F)
{
  Fs = F;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of San24

ASKER

Thanks all for the feedback. I was thinking about this a bit more and I was thinking instead of passing Forms as arguments to classes, I can take a different approach.

Here is what I want to do.

Lets think about it this way, I have a File and this can be of different types, lets call them Modules. These Modules need to inherit some of the base properties of the File.

I need a good way of propagating information between the File and the Modules.

So, I was thinking of two ways of doing this.

Approach A] I could use Inheritance. Since UserControls can`t be inherited. I was thinking, I`ll create a Class for each UserControl and then use Inheritence.

Example - FileUserControl will have an instance of FileClass.
                 ModuleA will have an instance of ModAClass which inherits from the FileClass.

I`m guessing the advantage would be I`m separating the UI and the logic.

Is that a good approach.

Approach B] I pass parameters into the Constructors. Look at the code below.

or Should I just pass Forms as arguments?




/// <summary>
    /// Main Form
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public FileUCntrl FUCntrl = null;
        public enum ModuleType { A, B }

        /// <summary>
        /// Lets try loading the a file of Type A
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            FUCntrl = new FileUCntrl("EExchange", ModuleType.A);
            MainPanel.Controls.Add(FUCntrl);
        }
    }


***********FileUserControl**************
    public partial class FileUCntrl : UserControl
    {
        public ModuleA ModA = null;
        public ModuleA ModB = null;
        public Form1.ModuleType MType;
        public string FName = string.Empty;

        public FileUCntrl(string fName, Form1.ModuleType type)
        {
            InitializeComponent();
            FName = fName;
            MType = type;
        }

        private void LoadBut_Click(object sender, EventArgs e)
        {
            switch (MType)
            {
                case Form1.ModuleType.A:
                    ModA = new ModuleA(FName, false);
                    FilePanel.Controls.Add(UA);
                    break;

                case Form1.ModuleType.B:
                    ModB = new ModuleB(FName, false);
                    FilePanel.Controls.Add(UB);
            }
        }
    }


*********ModuleA****************
  public partial class ModuleA : UserControl
    {
        public string FName = string.Empty;

        public ModuleA(string fName, bool B)
        {
            InitializeComponent();
            FName = fName;
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            Console.WriteLine(">>" + FName);
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of San24

ASKER

@Keeghi - Yes definitely interested. I think having a loosely coupled UI and Logic might be the way to go, I`m not sure how to go about it and what good a approach is. An example might be of great help.