HowTo: Understanding Interfaces – a simple description

image Interfaces are an important feature for designing great software, but many programming newbies have a understanding problem – Why should I use "interfaces"? What is an "interface"?

 

A very simple sample
I would like to create 3 types (Train, Car, Human) in my (very simple, not real world) sample. Each type is movable and that´s why I want to create the "IMovable" interface. 
To move these types I implement a  "God" class which can move these objects as he wishes. (I know – it´s a very real sample ;) ).

Structur:

image

The most important thing is our "IMovable" interfaces (the name of an interface begins with "I" in .NET):

    public interface IMovable
    {
        void Move();
    }

You define methods, properties or events in the interface – it´s a kind of a contract and the real implementation doesn´t matter.

    public class Human : IMovable
    {
        #region IMovable Member

        public void Move()
        {
            Console.WriteLine("The human take a step.");
        }

        #endregion
    }

Our human implements the interface – and "take a step" to "Move". The signature of the methods must be equal to the method of the interface (parameters, return value)!

Implementing god

Now it´s time to move! We implement now our God class with the "MoveObject" method. It takes an item which implement the "IMovable" interface:

    public class God
    {
        public static void MoveObject(IMovable item)
        {
            Console.WriteLine("God moves something...");
            item.Move();
        }
    }

The method "MoveObject" takes anything that implements the interface – the concret type doesn´t matter! You can now put a car, train, human or another type in it – as long as it implements the "IMovable" interface!

image

Now we can create many classes which implement the interface and we don´t need to change anything in our God class!

Our test program:

    class Program
    {
        static void Main(string[] args)
        {
            Car BMW = new Car();
            Train ICE = new Train();
            Human Robert = new Human();

            God.MoveObject(BMW);
            God.MoveObject(ICE);
            God.MoveObject(Robert);

            Console.ReadLine();
        }
    }

We have a train, a BMW and me – the result (the original source code was in german, I just translated it in this post) :

image

[ Download Source Code ]

Read more

Live Framework SDK is now public

The Live Framework is the API for Live Mesh. Since the PDC last year you could register for the Live Framework key to download the SDK. With the new update you have now public access to the SDK & Visual Studio Tools: Live Framework SDK April 2009 CTP Live Framework Tools for Microsoft Visual Studio …

Read more

HowTo: First steps with the Windows Azure CTP and the first "HelloWorld"

In my last blogpost I talked about different cloud computing providers, like Google, Amazon and Microsoft. Today I want to describe Windows Azure more and show you how to use the Windows Azure CTP and how to publish a "Hello World" app in the "cloud". Azure Service Platform  The Azure Service Platform use "Windows Azure" …

Read more

HowTo: Hello "Cloud-Computing"

Cloud-Computing is a new nice buzzword after Web 2.0 and everything goes "in the cloud" – but what means "in the cloud"? Who are the big players and why should you care about it?   Applications "in the cloud"? The big players are Google, Microsoft or Amazon (from my developer perspective). Todays applications are mostly …

Read more

25 Years Microsoft Press – 2 books for free

Microsoft Press celebrates his 25th anniversary and offer you two books. These offers expire on February 25, 2009. If you want to download the books you have to register to the MS Press Book Connection Newsletter: Microsoft® Visual C#® 2008 Express Edition: Build a Program Now! Windows Vista Resource Kit, Second Edition

Read more

HowTo: Create SQL Table Relationships via Drag´n´Drop

If you create a relationship between to SQL tables, you get many benefits. The most important benefit (for me) is the integrity of your data. Besides the database-world there is another huge benefit: The relationships are used by many O/R mappers to create a structured object model. You could create such realtionships via different dialogs …

Read more

HowTo: SQL Database web access with ASP.NET Dynamic Data and Entity Framework (hosted on IIS 6.0)

I started my programming career with PHP and MySQL and I used very often a webbased mysql admin panel called "PhpMyAdmin". It has (of course) not all features of the SQL Management Studios, but if I only want to have a quick look at the database it is very cool – and you only need …

Read more

ASP.NET MVC 1.0 RC published

The ASP.NET MVC 1.0 Release Candidate was today published by Scott Gu. Scott wrote a great blogpost about the improvements in the RC in his blog. The RC has many small improvements: Easier work with Visual Studio and MVC – "Go to Controller" / "Go to View" / "Add View" and so on AJAX improvements …

Read more

Windows 7 Beta – First impressions

Last week was the public release of the Windows 7 Beta – you can download it here. In installed it on my ThinkPad R61 and take a look at it. Here are my first impressions. Is it better than Vista?   A note about the performance Windows 7 runs good on my machine and boot …

Read more

Monomentale games

Mono as a game platform? With Unity 3D you can create games and even publish it on the Wii or the iPhone/iPod Touch. First mono-based games are now in the stores. iPhone/iPod Touch + Mono: The first mono-based iPhone/iPod Touch game in the AppStore: Raptor Copter Teaser (iPhone) from Flashbang Studios on Vimeo. iTunes Link. …

Read more

Recent Posts

  • Json-Online-Tools: Viewer & Json2Csharp generator

      Wherever APIs are mentioned the JSON format I not far away. Since I’m using two tools regularly I would like to introduce you to them. JSON Viewer If you only see the JSON-Text you are usually not able to see the structure. With the help of JSON Viewer you can have an easy overview: ...

  • Windows Phone SDK & „System“-Icons

      Although the Metro Design focuses a lot on Typography Icons are still quite important. If you install Windows Phone SDK you will receive 36 Icons. You can find them here: C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons Unfortunately many Icons you might know from the common applications are not integrated. Pedro Lamas extracted 99 additional Icons ...

  • image1830-570x194.png
    How can I figure out if my ADFS 2.0 works?

      I was working with ADFS 2.0 (“Active Directory Federation Services”) for a while when this simple question crossed my mind: How can I figure out if the connection between ADFS and AD “works”? Here is a simple test… What is ADFS? If you need some “position of trusts” beneath the AD-boarders you choose an ...

  • Subdomain vs. Subdirectory

      Better blog.mydomain.com or mydomain.com/blog? Good question! If got asked this question again via Twitter on the weekend so therefore I decide to share my experiences:   Choose a subdomain, if…. - You plan to offer “different services” which are “logical separated” on one domain - You are able to influence the subdomains without much ...

  • image1792-450x194.png
    Guide to Claim-Based Identity with the Access Control Service

    Microsoft published a free PDF about Claim-based Identity, Access Control Service and how to connect it to the remaining Microsoft world (Sharepoint, ADFS, Azure): Download-Link or MSDN Link I found the announcement today on the blog of Vittorio Bertocci. More information’s? If you want to get deeper into the subject you should risk a look ...

Support us