【问题标题】:Keyword 'this' (Me) is not available calling the base constructor调用基本构造函数时关键字“this”(我)不可用
【发布时间】:2011-01-27 15:11:54
【问题描述】:

在继承的类中,我使用基构造函数,但不能使用类的成员调用此基构造函数。

在此示例中,我有一个 PicturedLabel,它知道自己的颜色并具有图像。 TypedLabel : PictureLabel 知道它的类型,但使用基色。

使用TypedLabel的(基础)图像应该用(基础)颜色着色,但是我无法获得这种颜色

错误:关键字“this”在当前上下文中不可用`

解决方法?

/// 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 有趣的问题和很好的有效例子

标签: c# .net vb.net oop


【解决方案1】:

这个错误确实很有意义。

如果允许您以这种方式使用this,则会出现时间问题。您期望 LabelColor 具有什么值(即,它何时初始化)? TypedLabel 的构造函数尚未运行。

【讨论】:

  • 正如您在基类中看到的那样,我在无参数构造函数中对其进行了初始化。最后,假设我默认声明public Color LabelColor = Color.Red;
  • 但是您仍在调用该基本 ctor 的过程中,因此您在设置之前使用 LabelColor。
  • 使用初始化程序声明它可以解决这个问题,但这不会使this 可以安全使用。
  • 我认为这是对非常明显的编译错误的解释,但不是解决问题的方法。这是一个有效的问题,作者在这里寻求解决方法。
  • 解决方案非常简单,使用base中的默认ctor并分配body内部的图片。
【解决方案2】:

属性 LabelColor 目前没有初始化,所以它会为空。实际上,此时“this”并未初始化,因为在初始化“this”之前调用了基础构造函数,这就是无法调用“this”的原因。

【讨论】:

  • 我明白了。但正在寻找解决方案。
  • 或许将LabelColor作为参数传递给构造函数?
  • 也许是一个好方法,但不适合我的情况。基类定义了自己的颜色。
【解决方案3】:

您正在尝试访问尚未初始化的成员。 this.LabelColor 调用不可用的基类成员:当您编写: base(...)时,您还没有调用基类构造函数@

 public TypedLabel(LabelType type)
        : base()
    {
        pb.Image = GetImageFromType(type, this.LabelColor);
    }

【讨论】:

  • 是的......是的......但是。颜色在基类中,但过程在派生类中。
  • 您必须使图片框受到保护或提供受保护的属性才能访问其中的图像以供派生类使用。
【解决方案4】:

我认为作为一种解决方法,我将按如下方式实现:

public class PicturedLabel : Label
{
    protected Image
    {
        get {...}
        set {...}
    }
    ............
}

public class TypedLabel : PicturedLabel
{
    public TypedLabel(LabelType type)
       :base(...)
    {
       Type = type;
    }
    private LabelType Type
    {
      set 
      {
         Image = GetImageFromType(value, LabelColor);
      }
    }
}

已编辑:我将此上下文的 Type 属性设为私有,但它也可以是公共的。实际上,您可以将 Type 和 LabelColour 公开,并且每当用户更改这些属性中的任何一个时,您都可以重新创建图像并将其设置为您的基类,这样您就可以始终保证在图片框中使用有代表性的图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2014-07-01
    相关资源
    最近更新 更多