Here we got the second application request (submitted by Maciek). He asked if it is possible to implement a general application which can show a mathematical equation like y = x^3 + x^2 + 2 + 5. I think this is a very difficult topic and what I have done is to illustrate the idea by a simple example. Hope this application will give you some inspirations.
In this sample, the red dot is constantly moving fore and back. The y position is determined by an internal function locusFunction. However, the locusFunction is a function pointer only and it points to some functions defined by me. In the Silverlight, I have implemented 4 different locus function. You may also use this framework to implement your own locus easily.
Comparison
Flash implementation: 40 minutes
Silverlight implementation: 1 hour 20 minutes (Implemented First)
What’s the difference?
- Passing Function as parameter: var functionPointer:Function [AS3] vs delegate void functionPointer() [C#]
Source codes
Mathematical Locus [Flash 9, AS3] (12.2 KiB, 1,016 hits)
Mathematical Locus [Silverlight 2, C#] (19.9 KiB, 1,478 hits)
Flash
Silverlight
Passing Function as parameter: var functionPointer:Function [AS3] vs delegate void functionPointer() [C#]
Since AS3 is a weak function, using function pointer is easily since you don’t need to care about the return type and the number of parameters. But of course, it makes debug much more difficult.
Well, I think weak language is one of the major factory that makes Flash so popular. Through the code may be insecure, but at least you can achieve the result easily.
// AS3
var functionPointer:Function;
function flash(version:String):void{
trace(version);
}
function silverlight():String{
return "beta 2";
}
// trace "cs3"
functionPointer = flash;
functionPointer("cs3");
// trace "beta 2"
functionPointer = silverlight
trace(silverlight());
While in C#, it’s completely another story. You need to create a delegate type first and everything must be matched. It’s really a bit complicated, let’s take a look at the sample below.
// C#
delegate void newType(string version);
newType functionPointer;
void flash(string version){
System.Console.WriteLine(version);
}
string silverlight(){
return "beta 2";
}
// output "cs3"
functionPointer = flash;
functionPointer("cs3");
// you will get an error for this
functionPointer = silverlight;
// try to do it in this way
delegate string anotherType();
anotherType newPointer;
newPointer = silverlight;
System.Console.WriteLine(newPointer());
