【发布时间】:2011-01-12 13:36:20
【问题描述】:
好奇的情况:
public class MyTextBox : TextBox
{
// I want use the same height for all MyTextBoxes
public new static int Height;
}
public Form1()
{
InitializeComponent();
MyTextBox mtb1 = new MyTextBox();
MyTextBox mtb2 = new MyTextBox();
mtb1.Multiline = true;
mtb2.Multiline = true;
mtb1.Location = new Point(50, 100);
mtb2.Location = new Point(200, 100);
mtb1.Size = new Size(50, 50);
mtb2.Size = new Size(150, 150);
Controls.Add(mtb1);
Controls.Add(mtb2);
mtb1.Text = mtb1.Height;
mtb2.Text = mtb2.Height;
// Error 1 Member 'WindowsFormsApplication9.MyTextBox.Height'
// cannot be accessed with an instance reference;
// qualify it with a type name instead
}
在 VB.NET 中也是这样
Public Class MyTextBox
Inherits TextBox
Public Shared Shadows Height As Integer
End Class
mtb1.Text = mtb1.Height ' Text will be "0" '
'Warning 1 Access of shared member, constant member, enum member or nested '
' type through an instance; qualifying expression will not be evaluated.
问题:
==
- 这个方法不能 用于隐藏 继承的公共成员 上课?有时这可能是 有用...
- 如何为所有成员使用相同的
Height?
【问题讨论】:
-
我还问如何为文本框使用公共/静态/共享高度
标签: c# .net vb.net inheritance