6

There isn't one called Pens, that's for sure, but it's too odd not to find it. Is it really absent, or am I being blind?

(I'm just looking for a convenience class - for example, Colors.Red and Brushes.Red is there, but for pens the shortest appears to be new Pen(Brushes.Red, 1))

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324

2 Answers2

1

There is not the associated Pens class because a Pen is identified by both a Brush and a Thickness rather than just one property.

If you have a need for a set of standard pens (based on some thickness) then you will have to create your own. Another approach would be to create a code generator to supply the appropriate pens.

Here is an example of a class which adds a Pen for each default Brush:

public class DefaultPens
{
    private Dictionary<Brush, Pen> pens;

    public double Thickness
    {
        get;
        private set;
    }

    // used like defaultPens[Brushes.Red]
    public Pen this[Brush brush]
    {
        get { return this.pens[brush]; }
    }

    public DefaultPens(double thickness)
    {
        this.pens = typeof(Brushes).GetProperties(BindingFlags.Static)
                                   .Where(pi => pi.PropertyType == typeof(Brush))
                                   .Select(pi => pi.GetValue(null, null))
                                   .ToDictionary(
                                       b => b,
                                       b => new Pen(b, thickness));
        this.Thickness = thickness;
    }
}
user7116
  • 63,008
  • 17
  • 141
  • 172
1

There is no Pens class since it would be redundant. (Although you're right: Colors and Brushes are somewhat redundant, too.)

Why? Well, just think about waht a pen is: its properties can be described by a stroke with (or: thickness) and a color (or: brush). Therefore, there is a Pen Constructor(Brush, double).

Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
  • 1
    I think they are all exactly the same amount of redundant, namely: completely :) There's just the convenience/verbosity aspect. – Roman Starkov Dec 28 '11 at 02:22