Codeine .Net RSS 2.0
# Friday, 17 August 2007

Take a look at the decTop.  Its a small computer weighing three pounds that can run Windows CE or linux.  It has 128mb of memory and 10GB for storage.  With four USB ports you could easily add a wireless usb stick for network connectivity and do some cool things.  I came across this on LifeHacker and my mind is full of ideas for it.  Plus at $100 its not to bad of a price.

Friday, 17 August 2007 06:00:00 (Central Daylight Time, UTC-05:00)  #    Comments [0] - Trackback - Save to del.icio.us - Digg This! - Follow me on Twitter
Original Posts
# Thursday, 16 August 2007

Well I thought I would be nice and put up the code sample for implementing FizzBuzz using and extension method in VB.NET.  To read my full explanation of FizzBuzz and extension methods please read my previous post.

Those of you that know me, know that I could care less if you program in VB or C#, but I definitely prefer C# because it allows me to type a lot less.  Extension methods appear to be no different are a bit more complicated in VB, requiring that they are in a module and decorated with an attribute.

Module ExtensionMethods

    Sub Main()


        For i As Integer = 1 To 100
            Console.WriteLine(i.FizzBuzz)
        Next

        Console.ReadLine()

    End Sub

    <System.Runtime.CompilerServices.ExtensionAttribute()> _
Public Function FizzBuzz(ByVal Value As Integer) As String
        Dim rtnVal As String = ""

        If Value Mod 3 = 0 Then
            rtnVal += "Fizz"
        End If

        If Value Mod 5 = 0 Then
            rtnVal += "Buzz"
        End If

        If rtnVal = "" Then
            rtnVal = Value.ToString
        End If

        Return rtnVal
    End Function

End Module

Thursday, 16 August 2007 06:00:00 (Central Daylight Time, UTC-05:00)  #    Comments [0] - Trackback - Save to del.icio.us - Digg This! - Follow me on Twitter
Original Posts
# Tuesday, 14 August 2007

For those of you that don’t know what FizzBuzz is, it became quite popular awhile back when Jeff Atwood posted to his blog a few quotes from people about interviewing candidates for programming jobs and the fact that many of them can’t code. (It’s possible that the topic originated from someone else.  I heard it originally from Scott Hanselman’s blog who referenced Jeff.)  FizzBuzz is a simple coding exercise where you write a loop that prints the numbers from 1 to 100, except if the number is divisible by three it outputs Fizz and if the number is divisible by five it outputs Buzz.  If it is divisible by both you output FizzBuzz.

What is an Extension Method?  An extension method is a new feature in .Net 3.5 that allows you to add methods to an existing object.  It allows you to modify an object without needing to create your own version of it through inheritance.  Why is this useful?  I’m sure there are many reasons.  One is that if you don’t have the ability to change the object that is being passed to your class then you can’t just use inheritance and create your own version of the object, but what you can do is create an extension method.

This example that I am going to walk you through will add a method to int called FizzBuzz.  Calling this method will output a string with either the number of your int, Fizz, Buzz, or FizzBuzz depending on the criteria stated above.

This code for the extension method is fairly simple:

 

namespace ExtensionMethodExample

{

    public static class CustomExtensionMethods

    {

        public static string FizzBuzz(this int value)

        {

            string rtnVal = "";

 

            if (value % 3 == 0)

            {

                rtnVal += "Fizz";

            }

 

            if (value % 5 == 0)

            {

                rtnVal += "Buzz";

            }

 

            if (rtnVal == "")

            {

                rtnVal = value.ToString();

            }

 

            return rtnVal;

 

        }

 

    }

}

 

Here I have a static class called CustomExtensionMethods. Next, I have created a static method called FizzBuzz.  The magic happens when I all this to the input parameter. I then calculate FizzBuzz on my input parameter and return the appropriate string.  (I’m sure there is a better way to implement FizzBuzz.)

That’s it.  I then utilitize the new FizzBuzz method like this(In my case I have created a simple console application):

using System;

using System.Collections.Generic;

using System.Text;

using ExtensionMethodExample;

 

namespace ExtenderMethodExample

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i < 100; i++)

            {

                Console.WriteLine(i.FizzBuzz());

            }

 

            Console.ReadLine();

        }

    }

}

It’s that simple.  Drop this into a VS2008 console application and try it out.  You will have complete access to your extension methods via intellisense.  Happy coding.

 

Was this post helpful? Post a comment and let me know.

Tuesday, 14 August 2007 05:00:00 (Central Daylight Time, UTC-05:00)  #    Comments [0] - Trackback - Save to del.icio.us - Digg This! - Follow me on Twitter
Original Posts

Navigation
Archive
<2007 August>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2018
David A. Osborn
Sign In
Statistics
Total Posts: 70
This Year: 0
This Month: 0
This Week: 0
Comments: 33
Themes
Pick a theme:
All Content © 2018, David A. Osborn
DasBlog theme 'Business' created by Christoph De Baene (delarou)