Keyword 'this'(Me) is not available calling the base constructor

Posted by serhio on Stack Overflow See other posts from Stack Overflow or by serhio
Published on 2010-03-15T10:35:27Z Indexed on 2010/03/15 10:39 UTC
Read the original article Hit count: 443

Filed under:
|
|
|

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

In this example I have a PicturedLabel that knows it's own color and has a image. A TypedLabel : PictureLabel knows it's 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;
    }
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about oop