views:

683

answers:

3

I ran into a strange issue over the weekend while I was working on an asp.net mvc project in vb.net. I created an extension method to convert an integer to the corresponding month it is associated with. I tested the extension method in a console application so I know it is working.

In my asp.net mvc project I have a view and want to call the extension method but I get an error that the extension method is not recognized. I imported the namespace it was contained in and still couldn't shake the error. Any idea what's going on? I don't have my code with me, but I can post that tonight if it will help. Thanks!

Extension Method:

Imports System.Runtime.CompilerServices

Module SiteExtensions
    <Extension()> _
    Public Function ConvertToMonth(ByVal monthNumber As Integer) As String
        Dim month As String = String.Empty
        Select Case monthNumber
            Case 1
                month = "January"
            Case 2
                month = "February"
            Case 3
                month = "March"
            Case 4
                month = "April"
            Case 5
                month = "May"
            Case 6
                month = "June"
            Case 7
                month = "July"
            Case 8
                month = "August"
            Case 9
                month = "September"
            Case 10
                month = "October"
            Case 11
                month = "November"
            Case 12
                month = "December"
        End Select
        Return month
    End Function
End Module

View:

<%  For Each m As Integer In DirectCast(ViewData("Months"), IEnumerable)%>
<a href="#"><%=m.ConvertToMonth()%><br /></a>
<%Next%>

Error is: "ConvertToMonth is not a member of Integer"

Jon

+2  A: 

The code would probably help, but it still doesn't sound like you have the extension method imported correctly. You may want to try giving it the same namespace as your application. Or change the namespace of the extension to System.Web.Mvc, since we know that namespace works, and then back your way out from there.

Nick Berardi
Thanks for your response! I haven't been able to test it yet since my motherboard died as soon as I turned on my machine last night. It should be fixed though today or tomorrow and I'll give your suggestion a shot, thanks!
Jon
+5  A: 

Make sure you declare your module as Public.

David Findley
I lost a lot of time over this issue. I actually corrected it on my own and afterwards read this. +1
Dragoljub
+1  A: 

change the code like this:

Public Module SiteExtensions

End Module
mohamadreza