January 05, 2009   |  Register  |  Login
 
 

Money I would have
if those email scams
were legit:$9,210,000



 

Archive Minimize
Search Minimize
 
  You are here :- Blog
Welcome To CTRL-ALT-313373 Minimize

Welcome to CTRL-ALT-313373.com, a blog dedicated to things I've learned while wandering through the world of .NET.

Implementing FizzBuzz Using an Extension Method in VB.NET Minimize
Location: BlogsCTRLALT313373.com    
Posted by: dosborn Thursday, August 16, 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

Copyright ©2007 David Osborn
Permalink |  Trackback