[英]C# Create a general method parameter
I have program with 2 classes and i'm trying to create a method, which formats some System.Windows.Forms
objects of the other class. 我有2个类的程序,并且我正在尝试创建一个方法,该方法可格式化其他类的一些
System.Windows.Forms
对象。
This is my code: 这是我的代码:
internal void Format(Panel component, int width, int height, int x, int y)
{
component.Width = width;
component.Height = height;
component.Left = x;
component.Top = y;
}
internal void Format(GroupBox component, int width, int height, int x, int y)
{
component.Width = width;
component.Height = height;
component.Left = x;
component.Top = y;
}
internal void Format(Button component, int width, int height, int x, int y)
{
component.Width = width;
component.Height = height;
component.Left = x;
component.Top = y;
}
I can create the same methods (with different object parameter) for all required object types, but maybe there is a way to create it with just one method with 'general/overall/common' parameter for all object types . 我可以为所有必需的对象类型创建相同的方法(具有不同的对象参数),但是也许有一种方法可以对所有对象类型仅使用一个带有“ general / overall / common”参数的方法来创建它 。
Try using Control
as your parameter data type since all controls inherit from this class. 尝试将
Control
用作参数数据类型,因为所有控件都继承自此类。
internal void Format(Control component, int width, int height, int x, int y)
{
component.Width = width;
component.Height = height;
component.Left = x;
component.Top = y;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.