关键字'这个'(我)不提供调用基构造 [英] Keyword 'this' (Me) is not available calling the base constructor

查看:109
本文介绍了关键字'这个'(我)不提供调用基构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在继承的类我用的是基类的构造,但我不能使用该类的成员调用此基础构造。

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

使用TypedLabel应与(基地)的颜色着色的(基地)的形象,但是,我不能获得这种颜色

  

错误:关键字这个不可用当前context`

一个解决办法?

  ///基类
公共类PicturedLabel:标签
{
    图片框PB =新的PictureBox();
    众彩LabelColor;

    公共PicturedLabel()
    {
        //初始化在这里以特定的方式
        LabelColor = Color.Red;
    }

    公共PicturedLabel(图片IMG)
        : 基础()
    {
        pb.Image = IMG;
        this.Controls.Add(PB);
    }
}

公共枚举LabelType {A,B}

///派生类
公共类TypedLabel:PicturedLabel
{
    公共TypedLabel(LabelType型)
        :基座(GetImageFromType(类型,this.LabelColor))
    //错误:关键字'这个'是不是在目前的情况下可用
    {
    }

    公共静态图像GetImageFromType(LabelType类型,颜色C)
    {
        图片结果=新位图(10,10);
        矩形REC =新的Rectangle(0,0,10,10);
        喷喷=新钢笔(C);
        图形G = Graphics.FromImage(结果);
        开关(类型){
            案例LabelType.A:g.DrawRectangle(笔,REC);打破;
            案例LabelType.B:g.DrawEllipse(笔,REC);打破;
        }
        返回结果;
    }
}
 

解决方案

我觉得作为一个解决办法,我会实现这个如下:

 公共类PicturedLabel:标签
{
    受保护的图像
    {
        得到 {...}
        组 {...}
    }
    ............
}

公共类TypedLabel:PicturedLabel
{
    公共TypedLabel(LabelType型)
       :基础(...)
    {
       TYPE =类型;
    }
    私人LabelType类型
    {
      组
      {
         图片= GetImageFromType(值,LabelColor);
      }
    }
}
 

EDITED:我让类型属性私人对于这方面,但它也可以是公共的。事实上,你可以做类型和LabelColour公众和每当用户更改其中的任何属性,你可以重新创建图像并将其设置为你的基类,让您可以始终保证重新presentative图像用在图片框

In the inherited class I use the base constructor, but I can't use the class's members calling this base constructor.

In this example I have a PicturedLabel that knows its own color and has an image. A TypedLabel : PictureLabel knows its type but uses the base color.

The (base) image that uses TypedLabel should be colored with the (base)color, however, I can't obtain this color

Error: Keyword 'this' is not available in the current context`

A workaround?

/// 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;
    }
}

解决方案

I think as a workaround I will implement this as below:

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);
      }
    }
}

EDITED: I make the Type property private for this context, but it can also be public. In fact you can make Type and LabelColour public and whenever user change any of these properties, you can recreate the image and set it to your base class so that you can always guarantee a representative image is used in the picture box

这篇关于关键字'这个'(我)不提供调用基构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆