|
The reason you can't access it is because you are passing in a Shape which does not have the relevant parameters because they are defined in the subclasses. If you know that you are passing in a certain type, then you just need to cast them before you try to access them. For instance, in the Circle strategy, your Calculate method could be converted to this:
double Calculate(Shape shape)
{
Circle circle = shape as Circle;
if (circle == null)
{
throw new InvalidOperationException("You didn't pass in a circle");
}
return Math.Pow(circle.Radius, 2)* 3.14;
}
This space for rent
|
|
|
|
|
Thanks, that's exactly what I needed.
|
|
|
|
|
This isn't what the strategy pattern is for anyway.
You could just define these methods on their respective classes, it doesn't make sense to calculate the area of a circle as if it was a square, and vice versa. So changing the strategy doesn't even apply to this situation. Even if you do apply it, a strategy is supposed is not supposed to work only conditionally, they're supposed to be usable interchangeably - that's their whole point.
|
|
|
|
|
Thanks for replying. I'll look into this topic more.
|
|
|
|
|
 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)
{
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)
{
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.
|
|
|
|
|
Thank you for your time, I would not have known that the Strategy pattern isn't suited for my situation if you hadn't shared your thoughts.
Can you please explain why "the strategy pattern isn't that great for things that require different parameters". I've checked the definition of the Strategy pattern and there wasn't anything that says it shouldn't be used on situations that are similar to mine.
The following are part of the definition of the Strategy pattern I've found online.
* "Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it." (Gang of Four)
* "Specifies a set of classes, each representing a potential behaviour. Switching between those classes changes the application behaviour. (The Strategy)
From my perspective all of my classes which inherit from the ICalculateAreaStrategy interface have the same exact method and it takes the same exact number and type of parameter no matter which class it's used in.
modified 17-Jan-17 13:40pm.
|
|
|
|
|
Liagapi wrote: vary independently from clients That's the point: in your case, a Circle requires the algorithm for a circle, not for a rectangle, etc.: the algorithm absolutely depends on the client.
Think of something different, say a chess game. How to find out the next move? You could use "brute force" calculating a few moves ahead, or you could implement some "artificial intelligence strategy". There could be external constraints, e.g. how much time you may use for calculating the next move, and hence apply different algorithms with different trade-offs.
Select the algorithm applicable for the current scenario - and with the next scenario use an algorithm for that scenario.
|
|
|
|
|
My first impulse would have been to use polymorphism:
Polymorphism (C# Programming Guide)
(The example looks very familiar).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I have a SQL table with a Started column that is of type DateTimeOffset.
In a Linq To Entities query I'm trying to query against a DateTime value:
.
.
.
where t.Started.Value.DateTime >= startDT
.
.
.
I get "The specified type member 'DateTime' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
I've never used a DateTimeOffset. What's the right way to query this field using Linq To Entities?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
There are many things you can't do with L2E - sadly, this is one of them. What you need to do is materialize the data into something like a list first, and then apply the filtering to the list.
This space for rent
|
|
|
|
|
Pete O'Hanlon wrote: What you need to do is materialize the data into something like a list first
I thought of that, but what this means is first querying the ENTIRE table into a list, then applying the date check. This obviously won't work.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Turns out that it works with Linq To SQL, but not Linq To Entities.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I know - that's why I said this is one of the things you can't do with L2E.
This space for rent
|
|
|
|
|
ya, no big deal. I'm just getting started to converting isn't an issue.
Thanks for the heads up.. Save me hours of Googling & fiddling.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
How about:
where t.Started >= startDT
The implicit cast from DateTime to DateTimeOffset will let the C# compile, and the L2E query should just work.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I would have to know what the offset is that I'm looking for, then add that to the DateTime I'm using as my paramater
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
So you want the rows where the local value of the DateTimeOffset is after a particular DateTime value?
Seems a bit strange to me, but you should be able to do something like this:
int startYear = startDT.Year;
int startMonth = startDT.Month;
int startDay = startDT.Day;
int startHour = startDT.Hour;
int startMinute = startDT.Minute;
int startSecond = startDT.Second;
...
where t.Started >= EntityFunctions.CreateDateTimeOffset(
startYear, startMonth, startDay, startHour, startMinute, startSecond,
SqlFunctions.DatePart("tz", t.Started))
...
EntityFunctions is defined in System.Data.Entity.Core.Objects in the EntityFramework assembly;
SqlFunctions is defined in System.Data.Entity.SqlServer in the EntityFramework.SqlServer assembly.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Good evening everyone I'm looking on google but I do not find a solution that allows me to display the checkbox check in datagridview in a line specify. The display should not be vertical as the result in this image.
|
|
|
|
|
|
How can I implement NLP in c# program? Is there any logic for extracting single/multiple words from one or more sentences?
For more understandability, I included a paragraph(Doctor's Note) here.
The patient x is suffering from fever. He has no headache, cough or neck pain. But he has back pain.
Moreover patient x suffers Sleeplessness.
<big>My requirement is</big>,
I need to iterate symptoms such as <big>fever, back pain and sleeplessness</big>. And
do not take symptoms <big>headache, cough and back pain</big> .
|
|
|
|
|
|
There is no package yet that can guarantee correctness. Unless the doctors adhere to a specific syntax you'll run into weird phrases.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
hey volks,
I would accomplish the following scenario:
I have a C# Application, it hast registered OS-Extension (*.fpf) and comes as ClickOnce.
Now i would have the ability to save variables into a "project-file" that is saved somewhere on the system and if i doubleclick the C# application starts and read the stuff of the project-file.
Any idea how to solve this is welcome...
Thanks in advance,
Matthias
|
|
|
|
|
Member 11045388 wrote: Now i would have the ability to save variables into a "project-file" that is saved somewhere on the system and if i doubleclick the C# application starts and read the stuff of the project-file. You register a new file type. See MSDN[^], or here[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
well, that is done so far, i registered a extension for my app and when i open the associated file (which contains strings (with line breaks) the application open correctly. What i don't know is how to use the text part inside the app now. When i use filestreamreader - how do i say how to use the lines of the file (the one that i double-clicked) ....
any Idea how to do this?
|
|
|
|