UserControl和继承 [英] UserControl and Inheritance

查看:76
本文介绍了UserControl和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,



请帮助我们



我有一个UserControl,它有一个类如下所示。此Class a方法称为DrawHistogram





Good Day,

Please your help in

I have a UserControl which have a class as shown below. This Class a method Called DrawHistogram


namespace Prog1
{
	/// <summary>
	/// Summary description for HistogramaDesenat.
	/// </summary>
	public class HistogramControlClass : System.Windows.Forms.UserControl
	{ ......// Codes 

        }
        public void DrawHistogram(long[] Values)
         {
            //Doing calculation 
         }
		
}





另一方面,我有一张以下表格





In other hand, I have a Form with the following

namespace Prog1.ImageHistogram
{
    class HistogramEqualization:PNM, HistogramControlClass
    {
//I need to call DrawHistogram here where I used using and multi base class but 
// Still unable to call it

     }
}



简单地说,我需要从中调用DrawHistogram表单,使用使用和Multibase类但仍然无法调用它。

任何帮助表示赞赏


Simply, I need to call DrawHistogram from the Form, using "using" and Multibase class but still unable to call it.
Any help is appreciated

推荐答案

两件事:



首先,在方法名称中放置一个方法:

Two things:

First, you put a method inside a namespace:
namespace Prog1
{
	/// <summary>
	/// Summary description for HistogramaDesenat.
	/// </summary>
	public class HistogramControlClass : System.Windows.Forms.UserControl
	{ ......// Codes 

        }
        public void DrawHistogram(long[] Values)
         {
            //Doing calculation 
         }		
}



你不能把一个方法放在命名空间里,总是必须把它放在一个类中。



其次, HistogramEqualization 派生自两个cla sses:


You can't put a method inside a namespace, you always have to put it inside a class.

Second, HistogramEqualization derives from two classes:

class HistogramEqualization:PNM, HistogramControlClass



这是不可能的,因为C#只支持接口的多重继承。如果 PNM 是一个接口,那么在这种情况下可以进行多重继承。如果 PNM 是一个类,那么就不可能。

解决方案



,将 DrawHistogram 方法放在 HistogramControlClass 中:


This is impossible, because C# does only support multiple inheritance for interfaces. If PNM is an interface, then multiple inheritance is possible in this case. If PNM is a class, then it is not possible.

Solution


First, put the DrawHistogram method inside the HistogramControlClass:

namespace Prog1
{
	/// <summary>
	/// Summary description for HistogramaDesenat.
	/// </summary>
	public class HistogramControlClass : System.Windows.Forms.UserControl
	{ ......// Codes 
             public void DrawHistogram(long[] Values)
             {
                //Doing calculation 
             }
        }		
}



然后,解决多重继承问题。仅当 PNM 界面时,才需要这样做。一种可能的解决方案是将 PNM 的属性/字段/方法移动到 HistogramControlClass



解决这些问题后,您将能够在 HistogramEqualization 中调用 DrawHistogram


Then, solve the multiple inheritance problem. This is only necessary if PNM is not an interface. A possible solution is to move the properties/fields/methods of PNM to HistogramControlClass.

After solving these problems, you'll be able to call DrawHistogram in HistogramEqualization.


您的代码片段显示D​​rawHistogram方法不是HistogramControlClass的一部分 - 它在类定义之外。因此,如果您的HistogramControlClass嵌套在不同的类中,那么DrawHistogram是该类的方法,而不是HistogramControlClass。



快速检查说这一切都有效:

Your code fragment shows the DrawHistogram method as not a part of the HistogramControlClass - it is outside the class definition. So if your HistogramControlClass is nested in a different class, then DrawHistogram is a method of that class, not HistogramControlClass.

A quick check says this all works:
public partial class Form1 : Form
    {
    ...
    private void MyMethod()
        {
        HistogramControlClass hcc = new HistogramControlClass();
        hcc.DrawHistogram(new long[10]);
        }
    }
public class HistogramControlClass : UserControl
    {
    public void DrawHistogram(long[] values)
        {
        }
    }
public class HistogramEqualization : HistogramControlClass
    {
    public void MyMethod()
        {
        DrawHistogram(new long[100]);
        }
    }


这篇关于UserControl和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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