在继承的类中,我使用基础构造函数,但不能使用调用此基础构造函数的类的成员。关键字'this'(Me)不可用调用基础构造函数
在这个例子中,我有一个PicturedLabel知道它自己的颜色,并有一个图像。 A TypedLabel : PictureLabel
知道它的类型,但使用基本颜色。
使用TypedLabel应与(基极)的颜色来着色的(碱)图像,但是,我不能得到这种颜色
Error: Keyword 'this' is not available in the current context`
一种解决方法?
/// base class
public class PicturedLabel : Label
{
PictureBox pb = new PictureBox();
public Color LabelColor;
public PicturedLabel()
{
// initialised here in a specific way
LabelColor = Color.Red;
}
public PicturedLabel(Image img)
: base()
{
pb.Image = img;
this.Controls.Add(pb);
}
}
public enum LabelType { A, B }
/// derived class
public class TypedLabel : PicturedLabel
{
public TypedLabel(LabelType type)
: base(GetImageFromType(type, this.LabelColor))
//Error: Keyword 'this' is not available in the current context
{
}
public static Image GetImageFromType(LabelType type, Color c)
{
Image result = new Bitmap(10, 10);
Rectangle rec = new Rectangle(0, 0, 10, 10);
Pen pen = new Pen(c);
Graphics g = Graphics.FromImage(result);
switch (type) {
case LabelType.A: g.DrawRectangle(pen, rec); break;
case LabelType.B: g.DrawEllipse(pen, rec); break;
}
return result;
}
}
+1有趣的问题和很好的有效的例子 – 2010-03-15 10:41:25