Passing parameters in the Form constructor, winfor

2020-07-17 06:15发布

I have a following inheritance hierarchy:

Class A : Form
Class B : Class A

Class A needs to be able to accept a parameter so that I can create the instance of Class B like this:

ClassB mynewFrm = new ClassB(param);

How do I define such a constructor in Class A?

thanks!

I am using Winforms in .net 3.5, c#

EDITED: Class A and Class B are defined as forms, using partial classes. So I guess this is turning into a question about partial classes and custom (overriden) constructors.

3条回答
祖国的老花朵
2楼-- · 2020-07-17 06:46

Here is a complete demo sample that demostrates required behaviour.

For the sake of ease your learning, I chose a string type parameter that you adjust to your case.

To test it, create a new Visual Studio *C#* project and fill program.cs with the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Stackoverflow
{

    public class ClassA : Form
    {
        public ClassA()
        {
            InitializeComponent();
        }

        public ClassA(string WindowTitleParameter)
        {
            InitializeComponent();
            this.Text = WindowTitleParameter;
            MessageBox.Show("Hi! I am ClassB constructor and I have 1 argument. Clic OK and look at next windows title");
        }

        private void InitializeComponent() // Usually, this method is located on ClassA.Designer.cs partial class definition
        {
            // ClassA initialization code goes here
        }

    }

    public class ClassB : ClassA
    {
        // The following defition will prevent ClassA's construtor with no arguments from being runned
        public ClassB(string WindowTitleParameter) : base(WindowTitleParameter) 
        {
            InitializeComponent();
            //this.Text = WindowTitleParameter;
            //MessageBox.Show("Hi! I am ClassB constructor and I have 1 argument. Clic OK and look at next windows title");
        }

        private void InitializeComponent() // Usually, this method is located on ClassA.Designer.cs partial class definition
        {
            // ClassB initialization code goes here
        }

    }

    static class Program
    {
        /// <summary> 
        /// The main entry point for the application. 
        /// </summary> 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // If you debug this code using StepInto, you will notice that contructor of ClassA (argumentless)
            // will run prior to contructor of classB (1 argument)

            Application.Run(new ClassB("Look at me!"));
        }
    }
}
查看更多
\"骚年 ilove
3楼-- · 2020-07-17 07:00

A constructor for class would look like this.

private Object _object1;

public ClassA(object object1)
{
    _object1 = object1;
}
查看更多
神经病院院长
4楼-- · 2020-07-17 07:04

For ClassA your constructor would look like

public ClassA(Object param)
{
    //...
}

and for ClassB it would look like

public ClassB(Object param) : base(param) 
{
    //...
}

where base(param) will actually be calling the ClassA constructor that accepts that parameter.

查看更多
登录 后发表回答