必要な動作を示す完全なデモ サンプルを次に示します。
学習を容易にするために、ケースに合わせて調整できる文字列型パラメーターを選択しました。
テストするには、新しいVisual Studio * C# * プロジェクトを作成し、 program.csに次のコードを入力します。
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!"));
}
}
}