Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, August 27, 2014 6:23 PM | 1 vote
I have the following sealed Cirlce class
public sealed class Circle
{
private double radius;
public double Calculate(Func<double, double> op)
{
return op(radius);
}
}
I need to calculate the circumference of a (an arbitrary) circle based on this class without modifying the class. Here is what I tried that is not working:
Circle cir = new Circle();
double circ = 2 * Math.PI * cir.Calculate(5, 5);
How can I calculate a circumference of a circle based on this scenario?
Rich P
All replies (6)
Wednesday, August 27, 2014 8:11 PM âś…Answered | 1 vote
Ok, let's clarify:
Facts:
- The radius-field is private, and you can't set it via a public member like a method/property.
- You don't want to change the class, but the radius
That means in other words you want to change a private field from outside of the class, which is not nice, but possible:
Circle cir = new Circle();
var field = cir.GetType().GetField("radius",BindingFlags.Instance|BindingFlags.NonPublic);
field.SetValue(cir, 5);
double circ = cir.Calculate((radius) => 2 * Math.PI * radius);
Ha, and now, did I manage to pass the skills test question? ;-)
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook
Wednesday, August 27, 2014 6:48 PM | 1 vote
Hi Rich,
you can do it like this with a lambda-expression:
Circle cir = new Circle();
double circ = cir.Calculate((radius) => 2 * Math.PI * radius);
But note that the radius-field is 0 by default. I tried it with 5 like this:
public sealed class Circle
{
private double radius = 5;
public double Calculate(Func<double, double> op)
{
return op(radius);
}
}
I got the correct result of 31.4...
Have fun.
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook
Wednesday, August 27, 2014 7:26 PM | 1 vote
Thank you for your reply. I really like the Lambda approach. And truth be told, this was a skills test question, and I can't modify the sealed class. In your solution you set radius = 5 inside the sealed class. How can the 5 be used for the radius from outside of the sealed class?
Rich P
Wednesday, August 27, 2014 8:25 PM | 1 vote
Thank you again. I believe that was the whole point of the exercise -- these questions are usually always esoteric -- logical programming and efficiency not exactly the goal -- testing to see what you DO know. This was actually a cool question (just too bad it was one of the ones I missed :).
Many thanks again.
Rich P
Thursday, March 12, 2015 6:23 PM | 1 vote
Hey
What if you're asked to use the following value
double radius = 7.5;
Then what?
Tuesday, April 14, 2015 4:40 PM | 2 votes
public static class CircleExtension
{
public static double SetRadiusAndCalculate(this Circle c, int radius)
{
return c.Calculate(y => { return 2 * Math.PI * radius; });
}
}
Circle x = new Circle();
double perimeter = x.SetRadiusAndCalculate(7.5);