F1234k
F1234k

Reputation: 69

How do I set specific properties for a subclass in C# .NET?

First of all, let me just state that I'm new to .NET so I apologize in advance if my question is too naive or the answer too straightforward. I did some research and tested different things but I can't get it to work. What I'm trying to do:

I have a lot of different tooltips in my form and I want them to have the same properties. For example:

mySampleTooltip.ShowAlways = true;
mySampleTooltip.IsBalloon = true;
mySampleTooltip.AutomaticDelay = 750;
mySampleTooltip.AutoPopDelay = 32767;

Instead of doing that for every single tooltip and pasting the code 100 times, I thought that there may be a more elegant solution. My idea was to create a subclass of the class ToolTip which I called PermaToolTip and create objects from this specific class (is that the right approach by the way or maybe there is a better way?).

My code looks like this:

public class PermaToolTip : ToolTip
{
    private bool _ShowAlways;
    private bool _IsBalloon;
    private int _AutomaticDelay;
    private int _AutoPopDelay;

    // SET ShowAlways Property for subclass
    public bool ShowAlways
    {
        get
        {
            return _ShowAlways;
        }
        set
        {
            _ShowAlways = true;
        }
    }
    // SET IsBalloon Property for subclass
    public bool IsBalloon
    {
        get
        {
            return _IsBalloon;
        }
        set
        {
            _IsBalloon = true;
        }
    }
    // SET AutomaticDelay Property for subclass
    public int AutomaticDelay
    {
        get
        {
            return _AutomaticDelay;
        }
        set
        {
            _AutomaticDelay = 750;
        }
    }
    // SET AutoPopDelay Property for subclass
    public int AutoPopDelay
    {
        get
        {
            return _AutoPopDelay;
        }
        set
        {
            _AutoPopDelay = 32767;
        }
    }
}

but it doesn't work. Specifically:

1) I get a green line below the property names and a warning that says: Warning: 'ResellerRatingsGUI.PermaToolTip.ShowAlways' hides inherited member 'System.Windows.Forms.ToolTip.ShowAlways'. Use the new keyword if hiding was intended.

2) I instantiate an object of the class PermaToolTip and it does not have the properties that I want it to have.

Answer to comment: it's not user-defined controls. It's .NET controls.

Any idea what I'm doing wrong?

Upvotes: 2

Views: 1912

Answers (3)

Sean H
Sean H

Reputation: 746

First, as stated in another answer, your subclass should not have members that have the same name as members in the base class, unless your intent is to override their behavior. And in that case, you should only override virtual or abstract members.

From a design perspective, I would suggest a different approach. It appears the only purpose for the subclass is to create a ToolTip instance populated with default values. It's not necessary to use inheritance to accomplish this. Rather, you should consider some sort of helper that would give you a ToolTip instance populated with defaults. Then you're not stuck with a concrete type that you don't necessarily want or need.

Example class:

public static class DefaultToolTip 
{
  public static void Create()
  {
    ToolTip tip = new ToolTip();
    tip.ShowAlways = true;
    tip.IsBalloon = true;
    tip.AutomaticDelay = 750;
    tip.AutoPopDelay = 32767;
    return tip;
  }
}

Usage:

ToolTip toolTip = DefaultToolTip.Create();

Upvotes: 3

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

Create your a ToolTip, then use SetToolTip, that way you don't need 100s of them, just handful or maybe even one.

Straight from MSDN

  private void Form1_Load(object sender, System.EventArgs e)
  {
     // Create the ToolTip and associate with the Form container.
     ToolTip toolTip1 = new ToolTip();

     // Set up the delays for the ToolTip.
     toolTip1.AutoPopDelay = 5000;
     toolTip1.InitialDelay = 1000;
     toolTip1.ReshowDelay = 500;
     // Force the ToolTip text to be displayed whether or not the form is active.
     toolTip1.ShowAlways = true;

     // Set up the ToolTip text for the Button and Checkbox.
     toolTip1.SetToolTip(this.button1, "My button1");
     toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
  }

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564451

The base class already has those properties. Doing what you're doing should cause the compiler to complain (since you're hiding the base class member without using new to denote you want to hide it).

You don't need to make new properties - just set the base class defaults how you want in your constructor:

public class PermaToolTip : ToolTip
{
     public PermaToolTip()
     {
        // Define defaults differently now
        this.ShowAlways = true;
        this.IsBalloon = true;
        this.AutomaticDelay = 750;
        this.AutoPopDelay = 32767;
     }
}

This will cause your class to use the ToolTip properties, but with different default values.

Upvotes: 8

Related Questions