Follow us on Twitter

MapPoint & F#: An Introduction


F# is functional language for the .NET framework that is based on OCaml. Originally developed by Microsoft Research (Cambridge), it has recently been released as a full product by Microsoft. Functional programming has been favored by academia for a long time, but it has never expanded beyond niche applications in the commercial world. Although functional programming promises to be safer, the lack of supporting libraries and standard inter-language interfaces has been a major hurdle. F# might prove to be a major force in the adoption of functional programming. F# programs can easily access the entire .NET framework, and are about as fast as comparable C# programs.

F# is also multi-paradigm. It can be used in a functional manner, but it can also be used as an imperative or objective oriented language. This allows you to easily switch to the most appropriate paradigm for the problem at hand.

Another point in F#’s favor is that of multi-threaded programming. F#’s objects and values are naturally immutable – making it much easier to write thread-safe code.

That is enough of an introduction to F#. If you would like to learn F#, there are already a number of very good books and websites. A good book that I recommend is Expert F# (Expert’s Voice in .Net) by Don Syme, Adam Granicz, Antonio Cisternino; published by Apress.

The following examples were typed into the F# command line environment (FSI.exe), but they can also be entered into a file and compiled using Visual Studio. Both are included in the F# download from Microsoft. When using the command line environment, each line is evaluated when it is completed with a ‘;;‘ token. F#’s responses are not displayed for clarity. F# uses // for one line comments.

Using Late Binding

Following on from the page about late binding and C#, we shall first look at calling MapPoint from F# using late binding. This uses a traditional imperative approach. It also does not require a COM interop file to be created, but it is more verbose and is weakly typed.

In the fsi command line environment, type:

And that is all there is to it. It is very similar to the C# version, although the casting syntax is a little strange.

Using Early Binding

Next we shall look at early binding. This is more convenient because it allows compile-time type checking. It is also more natural and less verbose.

First you should create a MapPoint COM Interop called “Interop.MapPoint.dll“, if you do not already have one. Normally you would use visual Studio to create this for you when you add a COM reference, but F# Projects in Visual Studio 2008 do not currently have this option. Therefore you should create it manually using the tlbimp.exe utility. You will need to change the paths, but an example usage is as follows:

We are now ready to use MapPoint in F#. First we need to reference the COM Interop that we have just created, and any .NET namespaces that we need:

Next we create a new MapPoint instance. This time we use a functional approach to also set the Visible property in the same line of code. This uses an intermediate object ‘app‘ which is “returned” to mappoint.

Next we will do something useful and create a pushpin. Although it is essentially an assignment, the assignment performs a number of operations to create a temporary pushpin object p. The balloon state is then set, and MapPoint’s view is moved to the pushpin. After all these operations are complete, the pushpin object p is returned as the value for oPin:

And that is it! I have only touched on functional programming and not done it justice. Readers interested in functional programming should read more about F#, or learn a pure functional language such as OCaml or Standard ML.

The second part of this article uses F# to work through an existing pushpin set, classifying pushpins according to their existing data fields and assigning pushpin symbols accordingly.

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">