Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关键词';这';(Me)调用基构造函数时不可用_C#_.net_Vb.net_Oop - Fatal编程技术网

C# 关键词';这';(Me)调用基构造函数时不可用

C# 关键词';这';(Me)调用基构造函数时不可用,c#,.net,vb.net,oop,C#,.net,Vb.net,Oop,在继承的类中,我使用基构造函数,但不能使用类的成员调用此基构造函数 在这个例子中,我有一个PicturedLabel,它知道自己的颜色,并且有一个图像。TypedLabel:PictureLabel知道其类型,但使用基色 使用TypedLabel的(基本)图像应该使用(基本)颜色进行着色,但是,我无法获得这种颜色 错误:关键字“this”在当前上下文中不可用` 解决办法 /// base class public class PicturedLabel : Label { Picture

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

在这个例子中,我有一个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;
    }
}

这个错误确实很有道理


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

属性LabelColor目前尚未初始化,因此它将为null。事实上,“this”此时未初始化,因为在初始化“this”之前会调用基构造函数,这就是调用“this”无法完成的原因。

您正在尝试访问尚未初始化的成员。this.LabelColor调用不可用的基类成员:在编写
:base(…)


我认为作为一种解决办法,我将按以下方式实施:

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和LabelColor设置为公共,无论用户何时更改这些属性,您都可以重新创建图像并将其设置为基类,这样您就可以始终保证在图片框中使用具有代表性的图像

+1有趣的问题和很好的有效示例,正如您在基类中看到的一样,我在无参数构造函数中初始化它。最后,假设默认情况下我声明
公共颜色LabelColor=Color.Red
但您仍在调用该基ctor的过程中,因此您在设置它之前使用LabelColor。使用初始值设定项声明它可以解决此问题,但这不会使
安全使用。我认为这是对编译错误的一种解释,这很明显,但不是问题的解决方案。这是一个有效的问题,作者要求在这里解决。解决方法非常简单,使用默认的基向量,并在主体内指定图片。我理解。但是寻找一个解决方案。也许将LabelColor作为构造函数的参数?也许是一个好方法,但不适合我的情况。基类定义自己的颜色。是的。。。是 啊然而。颜色在基类中,但过程在派生类中。必须使图片框受保护或提供受保护属性才能访问派生类中的图像。
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);
      }
    }
}