Archive for August, 2009

Reference parameters

Normally instances of primitives (int, double, char etc.) and other value types are copied when assigned to another variable. The same when passing them as parameters.

This method is a proof of that:

public static void DoSomething(int ptr)
{
    ptr = 2;
}

By calling it you will notice that the value of the variable that is passed won’t be changed even though we assign another value to it.

int a = 3;
DoSomething(a);

Console.WriteLine(a);

In some cases you would like to pass a value variable as a reference. That to say that you want to pass the address of the value to modify the value of the variable passed as a parameter.

There are two ways of doing that in C#: the good old (sometimes dangerous) way and the new safe way. (The old one is the same as in C++)

The New Safe Way

Let’s start with the one you are supposed to know. I’m talking about the safe way. You can pass a parameter as a managed reference by marking the parameter as ref.

public static void DoSomething_Ref(ref int ptr)
{
    ptr = 2;
}

This is how you call it:

int a = 3;
DoSomething_Ref(ref a);

Console.WriteLine(a);

As you see the value of the variable passed as a parameter has changed when the method returns. That’s good. Remember that ref-parameters, as they are called, are always passed with the preceding ref-keyword. This keyword works for reference types as well but has no real use.

There is also an out-keyword which is essentially the same as ref but that the parameter needs to be assigned when the method returns.

The Good Old Way

You could use pointers to get the same behavior as ref-parameters. This is common in C and C++ programming. This is the C#-style, but be aware of that it is unmanaged code inside the unsafe-blocks. You are urged to use ref-parameters instead. They are managed and safer.

public static unsafe void DoSomething_Pointer(int* ptr)
{
    *ptr = 2;
}

Calling the method.

unsafe
{
    int a = 3;
    DoSomething_Pointer(&a);

    Console.WriteLine(a);
}

Conclusion

I have showed you two ways of passing value types as reference parameters.

Scenic Ribbon

In 2007 Microsoft introduced their new Ribbon UI in Office 2007 (whereas more commonly Office Ribbon). Later it was integrated into Windows Vista as a native API. Despite being a part of the operating system it was not used by any application in it neither has many developers fully embraced it… now it is time for change.

Windows 7 RC (Swedish localization)

Windows 7 RC (Swedish localization)

Scenic Ribbon

The Ribbon is back in a new version called the Scenic Ribbon and it will be included into the next release of the Windows operating system… yes, Windows 7. Their it has been added into the new versions of Paint and Wordpad. As can be seen the tabbed ribbon looks (and it is!) pretty awesome. It fits nicely the an application and it allows you to easily find the tools that you need. For instance in Wordpad that now also has an UI that conforms with the one of its big brother Word.

Microsoft Word 2010The Scenic Ribbon will be part of the next version of Office Systems as well. Here you see how it fits into Word 2010. All Office-programs got it now, including Visio, which did not have it before.

API

There are currently two different APIs: the original native API and the managed Ribbon API for Windows Presentation Foundation (WPF). These two are not related to each other.

The WPF library works for any platform while the native only works for Windows 7, and now Vista.

The WPF Ribbon API can be downloaded here.  (Walkthrough)

Update for Windows Vista

It has recently been reported that Microsoft is going to release an update to Windows Vista which updates the old Ribbon into the new Scenic Ribbon. The release is set to be in October and  the use of the new version of the Ribbon UI in Vista remains to be seen.

IDG (at least in Sweden) has misguided people by saying that Vista is going to get the same UI as Windows 7. If you read the article you will understand that so is not the case.

Microsoft Small Basic

Small Basic (not to be confused with SmallBASIC) is a quite new language that has been around for some years. It is a modified version of old QBasic that has has been built as a introductory programming language for beginners and hobbyists. Introducing the main concepts like functions and control structures.

The Small Basic environment is built on the .NET Framework and  consists of the compiler, the libraries and the integrated development environment.

The language

The programming language is Turing complete and has all the features of a working language. It contains functions and control flow structures, including conditional branching. All variables are typed dynamic and no explicit typing is needed. Small Basic is not purely object-oriented but it does have the notion of objects. You can consume classes and objects in programs written in any other .NET language.

Just like its predecessor it is also an even-driven language.  You can hook-up to an event to trigger functions, so called event handlers.

For the sake of simplicity the language also only has 15 keywords.

The source code for a version of the compiler is provided in the sample pack of the Common Compiler Infrastructure (CCI).

The libraries

With Small Basic comes a little runtime and code library. It contains classes with functions for graphics and math operations. There are also functions for downloading random pictures from Flickr and looking up words in an online dictionary.

Combing the functions of the library with your own code you could for instance create your own little game or graphics program.

Small Basic comes with samples that demonstrate the possibilities of the language.

Turtle

The turtle gives you a Logo-like way of drawing graphics. This example draws a square:

For i = 1 to 4
  Turtle.Move(100)
  Turtle.TurnRight()
EndFor

The IDE

Small Basic comes with a simplified WPF-based integrated development environment (IDE) that provides code completion, or IntelliSense, while you write code. It also allows you to easily share your creation with other programmers by a press of a button.

Small Basic IDE

If you want to know more you can check the following links:

Soon…

Now it is less than one week left and it feels kind of strange that I will be moving soon. I mean to a place were I’ve never been before. Being all alone, on my own. Though it feels like I have made the right decision. It will be another experience and another challenge in life.

Last evening my old classmate Christer contacted me. Yesterday he moved into the building next to the one I’m going to live in. The apartments in the buildings are virtually the same. Christer told me that it is really nice in them. I can’t wait to go there now.

First I need to go shopping some things. My mom has promised me to go help me find what I need. I’m lucky to have her.

Wallander in Hammenhög 3

This morning my mother told me that they were filming at the inn near our home. I went by the place after I had been to the dentist and took some pictures. They had sadly enough finished when I came.

Update: On the inn’s blog they say that the inn is supposed to be the retirement home where Kurt Wallander’s father lives and that they were shooting nightscenes with the windows covered.

Read the previous post.

The inn in HammenhögThe inn in Hammenhög

Wallander in Hammenhög 2

Yesterday evening I tweeted that the trailers were back behind the supermarket. Well, today I went down to the center in the late afternoon and saw that they were shooting at Fröken Östergren’s café. The previous place is just across the street from the café. As you may see in the photo I passed by it. It is just on the other side of the street.

I’m not really surprised that they chose this café for this movie because I know that the owner of the café is involved in the filming. However I do wonder what kind of plot it is. Is it about Hammenhög? I’m sure going to find out when the film is released.

Anyway if you see a guy walking with a dog, a Collie, in a Wallander movie then it might be me.

Here’s my previous post about what is happening in our little town.

Update 09/08/18 – It’s the English Wallander TV-series with Kenneth Branagh.

DSC00383

Free ebooks and tutorials

Here comes a list of free ebooks and tutorials I have found on the Internet.

Ebooks

Tutorials

  • PublicJoe has written a complete tutorial for C# and the basics of the .NET Framework. It does not cover the latest features such as LINQ but it is very good for beginners. You can find the ebook here.
  • Christian Moser runs WPF Tutorial.net where he covers most of Windows Presentation Foundation (WPF).
  • Microsoft has its own Silverlight tutorial.

 

More to come in a future post…

Extensions methods

Deriving classes to extend the functionality of existing classes is a powerful feature of object-oriented programming. But there are cases when that is a rather inconvenient approach. It may also be impossible inheriting a class for instance if it is sealed. That is when extension methods come in handy.

With extension methods you can extend classes with new methods no matter if they are sealed or not. To do that you need to declare a static method in a static class. The first parameter of the method must be of the type to extend and prefixed with the keyword this.

public static class Extensions
{
    public static int Negate(this int i)
    {
        return -i;
    }
}

You can then simply call the method as if it was an ordinary class member. Cool huh?

int a = 2;
int b = a.Negate();

As seen here it is a much more convenient syntax.

Extension methods can also be a good alternative to e.g. decorator pattern.

Behind extension methods

It is just a compiler trick and it is called as any other static method with parameters, in this case it is the type that it extends.

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] int32 a,
        [1] int32 b)
    L_0000: nop
    L_0001: ldc.i4.2
    L_0002: stloc.0
    L_0003: ldloc.0
    L_0004: call int32 ConsoleApplication1.Extensions::Negate(int32)
    L_0009: stloc.1
    L_000a: ret
}

This is how the extension method really looks like. The only thing that differs from other static methods is the ExtensionAttribute.

.class public abstract auto ansi sealed beforefieldinit Extensions
    extends [mscorlib]System.Object
{
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
    .method public hidebysig static int32 Negate(int32 i) cil managed
    {
        .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
        .maxstack 1
        .locals init (
            [0] int32 CS$1$0000)
        L_0000: nop
        L_0001: ldarg.0
        L_0002: neg
        L_0003: stloc.0
        L_0004: br.s L_0006
        L_0006: ldloc.0
        L_0007: ret
    }

}

Wallander in Hammenhög

Yesterday evening I went out for a walk in my town and saw some trailers behind the local supermarket. I guessed that the filming team had returned here to shoot some more scenes in our beautiful town of Hammenhög.

Today I went down and took some photos  and it was confirmed that they have hired a premise here in Hammenhög. Now it looks rather nice but I haven’t  been able to see what it is supposed to be in the movie. My best guesses are that it is a shop or a café. (Update 09/08/14 – it is a flower shop called Runfeldts)

I don’t know if it is for the Swedish or British Wallander movies, or if it is for the same movie as the scenes they shot here last week. Which by the way was some march through the main street (seen in front of the building).

Bilfirma N.H. Nilsson. Set for Wallander.

Trailers behind the supermarket.

The building that is located near the town square was once the workshop and home of a car dealer who sold GM cars. The family still runs the firm here in town, but now they are specialized in Peugeot cars. (website)

The building has recently been the home of an old lady who passed away before summer.

Film set

Dynamic programming languages on the CLR

This article is about dynamic programming languages on the Common Language Runtime. The article is based on another article I have written 2 years ago. (Read notes: bottom)

There are a lot of compilers out there and the number is increasing. This has been very interesting in the last few years because of the development frameworks, which has removed the borders between machine architectures. One of these frameworks is the .NET Framework, which is Microsoft’s own competitor to other frameworks like the Java Platform. The .NET Framework was designed to support multiple languages and let them co-exist as equal languages on the same platform sharing the same class and runtime library.

However the Common Language Runtime (CLR) is build for statically typed languages only and dynamic languages can not run directly on the CLR. Now Microsoft has taken the next big step by developing a framework that makes this possible. I am talking about the Dynamic Language Runtime, which is becoming a part of .NET Framework 4.0 upon its release next year (2010).

Dynamic Language Runtime

The Dynamic Language Runtime (DLR) is not a runtime in the same sense as CLR. It is a library much like the rest of the Framework Class Library (FCL).
The DLR provides functionality that carries out dynamic dispatch at runtime as well as making it easy for the existing statically typed programming languages to interoperate with dynamically typed ones.

The DLR and the languages on it is not strictly bound to .NET Framework  and can be used with Mono too. The language teams are making sure that they are compatible with both.

I have previously written another article which I strongly suggest that you read it if you are interested in the DLR. It can be found here.

Web development

Web development is one thing that the dynamic programming languages are suitable for. With dynamic languages web applications can be developed quickly by testing as you write it. Dynamic languages are good scripting languages because you do not think of types as you do in statically typed languages. That may be the reason many people, especially hobbyists, start with learning a dynamic scripting language. PHP is one of, or maybe the most popular one.

Not to forget that ASP.NET and Silverlight can integrate the DLR and use any language upon it.

Programming languages

Here is a short introduction of each of the major dynamic programming languages that are targeted for the CLR.

One common thing for all languages is that they want to be compatible with the official implementation as well the .NET Framework.

IronPython

IronRuby is the official Python implementation for the DLR. It implements the language compiler as well as an interpreter and the Python libraries.

The DLR allows IronPython to bind to other languages, both managed CLI-languages and unmanaged native languages. Among those CPython, Ruby, JavaScript and Silverlight.

The project was started by Jim Hugunin who also created Jython (formerly JPython) on the Java Platform. The reason he created and Python implementation for the CLR was that he had heard that .NET was a bad platform to implement dynamic programming languages on. This was proven wrong. Hugunin later joined Microsoft and created the DLR from what he had learned from his previous experience. The DLR has since then been a dependant on the IronPython project at Microsoft which he still leads.

The prefix “Iron” is said to be taken from tales where the word and metal has a powerful, somewhat magical meaning.

IronRuby

IronRuby is a implementation of the Ruby programming language developed at Microsoft with the same goals as IronPython.

It was started by John Lam as RubyCLR, a Ruby binder for the CLR, and has since then evolved into a full language implementation for the DLR. Lam now works as the leader for the IronRuby team at Microsoft.

Phalanger

Phalanger (PHP Language Compiler) is an implementation of PHP which runs on the CLR. It compiles PHP code into .NET assemblies. The project is also supported by Microsoft.

The primary goal of Phalanger is to close the gap between PHP and .NET. It aims to be fully compatible with PHP all down to the runtime libraries and at the same time be compatible with the .NET Framework.

It is completely different from the other dynamic languages because it has its own runtime library and is not dependant on the DLR. Despite of that the platforms can interoperate and take advantage of each other.

The project was started by Tomas Matousek, a former student at the Charles University in Prague, in the Czech Republic. Matousek has left the project and is now working as a developer in the IronPython team at Microsoft.

Phalanger is dependant on its community.

Notes:

This is partly a re-written version of an article with the same name. Some parts are copied from the older one and changed to fit in the current one.

Next Page »



Robert Sundström

Tweets

  • Waiting for the VS 2010 Beta 2 to be released to public. Only a couple of hours left. But still not sure when. Is it local time? 1 month ago
  • Writing meaningless programs.. 1 month ago
  • I consider myself being a logical being, but my logic is flaud. 1 month ago
  • SvD: Facebook gör dig smart – men Twitter fördummar (http://bit.ly/14XGNr 2 months ago
  • 2 weeks have passed since I came to Karlskrona. I pretty much enjoy living here. College has started and so far little coursework. 2 months ago

Recent Comments

Steve on Windows 3.11 on a virtual…
catchmikey on Wakoopa
Hillary on First days in Delsbo
wilhol on PC or Mac?

Categories

Calendar

August 2009
M T W T F S S
« Jul   Sep »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Blog Stats

  • 15,577 hits