views:

1451

answers:

2

I have a base class that does calculations on image sizes. I'm deriving a class from that and have predefined image sizes that will be used in my code. While what I have works, I have a strong feeling that I'm not doing it properly.

Ideally, I'd like to just pass DerviedClass.PreviewSize as the parameter to GetWidth without having to create an instance of it.

class Program
{
    static void Main(string[] args)
    {
        ProfilePics d = new ProfilePics();
        Guid UserId = Guid.NewGuid();

        ProfilePics.Preview PreviewSize = new ProfilePics.Preview();
        d.Save(UserId, PreviewSize);
    }
}

class ProfilePicsBase
{
    public interface ISize
    {
        int Width { get; }
        int Height { get; }
    }

    public void Save(Guid UserId, ISize Size)
    {
        string PicPath = GetTempPath(UserId);
        Media.ResizeImage(PicPath, Size.Width, Size.Height);
    }
}

class ProfilePics : ProfilePicsBase
{
    public class Preview : ISize
    {
        public int Width { get { return 200; } }
        public int Height { get { return 160; } }
    }
}
+1  A: 

There are a few ways you could do this, depending on your needs. I would look at doing a different interface, setup. SOmething like this.

public interface ISizedPics
{
    int Width {get; }
    int Height {get; }
    void Save(Guid userId)
}
pubic class ProfilePics, iSizedPics
{
    public int Width { get { return 200; } }
    public int Height { get { return 160; } }
    public void Save(Guid UserId)
    {
     //Do your save here
    }
}

Then, with this done, you could actually work with it like this.

ISizedPics picInstance = new ProfilePics;
Guid myId = Guid.NewGuid();
picInstance.Save(myId);

This is just one way of doing it, I like this way, as you can easily create a factory class around this that helps you declare the instances as needed.

Mitchel Sellers
+5  A: 

It seems to me that you want a more flexible implementation of ISize - having an implementation which always returns the same value seems fairly pointless. On the other hand, I can see that you want an easy way of getting the size that you always use for a preview. I would do it like this:

// Immutable implementation of ISize
public class FixedSize : ISize
{
    public static readonly FixedSize Preview = new FixedSize(200, 160);

    private readonly int width;
    private readonly int height;

    public int Width { get { return width; } }
    public int Height { get { return height; } }

    public FixedSize(int width, int height)
    {
        this.width = width;
        this.height = height;
    }
}

You could then write:

ProfilePics d = new ProfilePics();
Guid userId = Guid.NewGuid();

d.Save(userId, FixedSize.Preview);

This would reuse the same instance of FixedSize whenever you called it.

Jon Skeet
That's exactly what I was looking for. Thanks so much! I don't know why I wasn't able to wrap my head around it.
Arthur Chaparyan
Jon - For the ProfilePics class to access the width and height though a public read only property must exist.
Mitchel Sellers
Whoops, yes - editing.
Jon Skeet