Click here to Skip to main content
15,847,549 members
Home / Discussions / C#
   

C#

 
QuestionGlade errors after launching debug Pin
Sascha Manns17-Jan-17 0:18
professionalSascha Manns17-Jan-17 0:18 
AnswerRe: Glade errors after launching debug [Solved] Pin
Sascha Manns17-Jan-17 6:41
professionalSascha Manns17-Jan-17 6:41 
QuestionImplementing Strategy Pattern Pin
Liagapi16-Jan-17 23:20
Liagapi16-Jan-17 23:20 
AnswerRe: Implementing Strategy Pattern Pin
Pete O'Hanlon16-Jan-17 23:46
subeditorPete O'Hanlon16-Jan-17 23:46 
GeneralRe: Implementing Strategy Pattern Pin
Liagapi17-Jan-17 0:22
Liagapi17-Jan-17 0:22 
GeneralRe: Implementing Strategy Pattern Pin
harold aptroot17-Jan-17 0:05
harold aptroot17-Jan-17 0:05 
GeneralRe: Implementing Strategy Pattern Pin
Liagapi17-Jan-17 3:25
Liagapi17-Jan-17 3:25 
AnswerRe: Implementing Strategy Pattern Pin
F-ES Sitecore17-Jan-17 0:18
professionalF-ES Sitecore17-Jan-17 0:18 
To answer your specific question you need to up caste to the type you need

public class Shape
{
    public string Name { get; set; }
}

public class Circle : Shape
{
    public double Radius { get; set; }
    public Circle(string name, double radius)
    { Name = name; Radius = radius; }
}

public class Square : Shape
{
    public double Side { get; set; }
    public Square(string name, double side)
    { Name = name; Side = side; }
}

public interface ICalculateAreaStrategy
{
    double Calculate(Shape shape);
}

public class CalculateCircleAreaStrategy : ICalculateAreaStrategy
{
    public double Calculate(Shape shape)
    {
        // "up cast" the Shape to a Circle, if shape is not a Circle then "as" will return null
        Circle c = shape as Circle;

        if (c == null)
        {
            return 0;
        }

        return 3.14 * c.Radius * c.Radius;
    }
}

public class CalculateSquareAreaStrategy : ICalculateAreaStrategy
{
    public double Calculate(Shape shape)
    {
        // "up cast" the Shape to a Square, if shape is not a Square then "as" will return null
        Square s = shape as Square;

        if (s == null)
        {
            return 0;
        }

        return s.Side * s.Side;
    }
}

public class CalculateAreaService
{
    readonly ICalculateAreaStrategy calculateAreaStrategy;

    public CalculateAreaService(ICalculateAreaStrategy strategy)
    {
        calculateAreaStrategy = strategy;
    }

    public double CalculateArea(Shape shape)
    {
        return calculateAreaStrategy.Calculate(shape);
    }
}


Usage

CalculateAreaService cas = new CalculateAreaService(new CalculateSquareAreaStrategy());

Console.WriteLine(cas.CalculateArea(new Square("Square", 3)));

cas = new CalculateAreaService(new CalculateCircleAreaStrategy());

Console.WriteLine(cas.CalculateArea(new Circle("Circle", 3)));

Console.ReadLine();


However the strategy pattern isn't that great for things that require different parameters such as you have with Square, Circle etc. I appreciate you're doing this to learn the strategy pattern and this might not be your ultimate use for it, but if you wanted to do something like you're doing then you could do it more simply like this

public interface ICalculateArea
{
    double Calculate();
}

public class CalculateCircleArea : ICalculateArea
{
    private Circle circle;

    public CalculateCircleArea(Circle circle)
    {
        this.circle = circle;
    }

    public double Calculate()
    {
        return 3.14 * circle.Radius * circle.Radius;
    }
}

public class CalculateSquareArea : ICalculateArea
{
    private Square square;

    public CalculateSquareArea(Square square)
    {
        this.square = square;
    }

    public double Calculate()
    {
        return square.Side * square.Side;
    }
}


Usage

List<ICalculateArea> calcs = new List<ICalculateArea>();
calcs.Add(new CalculateSquareArea(new Square("Square", 3)));
calcs.Add(new CalculateCircleArea(new Circle("Circle", 3)));

foreach (var calc in calcs)
{
    Console.WriteLine(calc.Calculate());
}


Or even simpler still have Square, Circle etc implement ICalculateArea and return their own area.
GeneralRe: Implementing Strategy Pattern Pin
Liagapi17-Jan-17 0:28
Liagapi17-Jan-17 0:28 
GeneralRe: Implementing Strategy Pattern Pin
Bernhard Hiller18-Jan-17 0:21
Bernhard Hiller18-Jan-17 0:21 
AnswerRe: Implementing Strategy Pattern Pin
Gerry Schmitz17-Jan-17 6:48
mveGerry Schmitz17-Jan-17 6:48 
QuestionConvert DateTimeOffset to DateTime in Linq To Entities Pin
Kevin Marois16-Jan-17 14:18
professionalKevin Marois16-Jan-17 14:18 
AnswerRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Pete O'Hanlon16-Jan-17 23:52
subeditorPete O'Hanlon16-Jan-17 23:52 
GeneralRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Kevin Marois17-Jan-17 6:08
professionalKevin Marois17-Jan-17 6:08 
GeneralRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Kevin Marois17-Jan-17 6:54
professionalKevin Marois17-Jan-17 6:54 
GeneralRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Pete O'Hanlon17-Jan-17 6:56
subeditorPete O'Hanlon17-Jan-17 6:56 
GeneralRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Kevin Marois17-Jan-17 6:57
professionalKevin Marois17-Jan-17 6:57 
AnswerRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Richard Deeming17-Jan-17 3:34
mveRichard Deeming17-Jan-17 3:34 
GeneralRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Kevin Marois17-Jan-17 6:19
professionalKevin Marois17-Jan-17 6:19 
GeneralRe: Convert DateTimeOffset to DateTime in Linq To Entities Pin
Richard Deeming17-Jan-17 9:29
mveRichard Deeming17-Jan-17 9:29 
QuestionShow checkbox check in the datagridview line Pin
abboudi_ammar16-Jan-17 11:53
abboudi_ammar16-Jan-17 11:53 
AnswerRe: Show checkbox check in the datagridview line Pin
Richard MacCutchan16-Jan-17 22:37
mveRichard MacCutchan16-Jan-17 22:37 
QuestionHow can I extract positive word(s) from a paragraph in c# -Natural Language processing . Pin
Nowfal Sharafudeen16-Jan-17 2:09
Nowfal Sharafudeen16-Jan-17 2:09 
AnswerRe: How can I extract positive word(s) from a paragraph in c# -Natural Language processing . Pin
Pete O'Hanlon16-Jan-17 2:21
subeditorPete O'Hanlon16-Jan-17 2:21 
AnswerRe: How can I extract positive word(s) from a paragraph in c# -Natural Language processing . Pin
Eddy Vluggen16-Jan-17 3:48
professionalEddy Vluggen16-Jan-17 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.