有关System.windows.Form.Control的怀疑 [英] Doubt regarding System.windows.Form.Control

查看:40
本文介绍了有关System.windows.Form.Control的怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我看到了如下所示的代码

Hi all,

I have seen a code as shown below

Class Sample
      {
           AddSubPage("Title",new ServerControl());
           private void AddSubPage(string title, Control subPage)
           {
          
           }
      };
      Class ServerControl
      {
         public ServerControl()
         {
             InitializeComponent();
         }
      };


我的疑问是关于第二个参数.
中实际传递的是什么
第二个参数.我知道控件"是所有控件形式的基类.
将新的ServerControl()分配给Control对象.

``new ServerControl()''意味着只调用一个构造函数(请参见代码).然后是什么

实际经过这里.

注意:ServerControl类继承了一个名为DBMigrationSubPageBase的类,该类

继承类"UserControl"

预先感谢


My doubt is regarding the second parameter.What is actually passing in the

second parameter.I know ''Control'' is the base class of all controls in form.So how

to assign new ServerControl() to Control object.

''new ServerControl()'' means just calling a constructor.(See the code).Then what is

actually passing here.

Note:Class ServerControl is inherits a class called DBMigrationSubPageBase which

inherits a class ''UserControl''

Thanks in advance

推荐答案

传递给AddSubPage的是您调用该方法的任何类型的实例,只要该实例是从Control派生的.否则,编译器将引发错误.一旦通过,您将必须显式转换为实际通过的控件类型.

如果您进行快速测试,则如下所示:-
What gets passed to AddSubPage is an instance of whatever type you call the method with, as long as it derives from Control. If not, the compiler will throw an error. Once it has been passed, you will have to explicitly convert to the type of control actually passed.

If you do a quick test, something like this:-
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AddServerControl("New Server Control", new ServerControl());
        }

        void AddServerControl(string title, Control subpage)
        {
            //Should do checking here to ensure it is Server control.
            ServerControl ctrl = subpage as ServerControl;
            ctrl.Talk("I am a Server control!");
        }
    }

    class ServerControl:Control
    {
        public ServerControl()
        {

        }

        public void Talk(string message)
        {
            MessageBox.Show(message);
        }
    }




您将看到显示消息框,这意味着传递的对象实际上是ServerControl的实例.

希望对您有帮助




you will see that the messagebox is displayed, which means that the object passed IS actually an instance of ServerControl.

Hope this helps


这篇关于有关System.windows.Form.Control的怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆