none
VB2008: How to import Module Extension Methods in a ASPX Page?

    Question

  • Hello,


    I have a module with Extension Methods like this stored in my App_Code Directory as "MyExtensions.vb" :

    ----------------------------------------------------------------------------------------------------------------
    Imports System.Runtime.CompilerServices

    Module MyExtensionMethods

        <Extension()> _
        Public Function IsValidEmailAddress(ByVal email As String) As Boolean
            Dim regex As New Regex("^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")
            Return regex.IsMatch(email)
        End Function

    End Module
    ----------------------------------------------------------------------------------------------------------------


    Now I want to import "MyExtensionMethods" in my aspx.vb File.

    When I type in my aspx.vb File on top "Imports MyExtensionMethods"
    I get this error message:
    "MyExtensionMethods is not accessible in this context because it is 'Friend'"


    How can I solve this problem?

    Many thanks for any help!

    Regards
    Joerg

    Sunday, December 02, 2007 1:07 PM

Answers

  • Use the Line

     

    Public Module MyExtensionMethods

     

    That should help you out.

     

    Sunday, December 02, 2007 3:58 PM
  • Hello,

     

    I have a module with Methods stored in my App_Code Directory as "MyExtensions.vb":

    -----------------------------------------------------------------------------------------------------------

    Public Module MyExtensions

     Public Function IsValidEmailAddress(ByVal email As String) As Boolean

     ....

     End Function

    -----------------------------------------------------------------------------------------------------------

     

    How can I add my Module "MyExtensions" to the web.config?
    Because I don't want to import it in all the vb files.

    I tried this in the web.config but it didn't work:
    <namespaces>
    <clear />
    <add namespace="MyExtensions"/>
    </namespaces>


    The error message is:
    A using namespace directive can only be applied to namespaces; 'MyExtensions' is a type not a namespace C:\MyProjects\MyTestApp\web.config

     

    Thanks in advance for any help.


    Regards

    Joerg

    Monday, December 03, 2007 7:47 PM
  • Your method is contained within a Module

    which both had to be public as the default access I think is freidn which was your first problem.

     

    Extension methods can be defined in namespaces either in code or using the root namespace of the assembly.  However to bring them into scope you would need to import using the Imports statement.

     

    If you want this applicable to all your code so you dont have to specify the imports all the time - you can use a project level imports on your project.  Project properties -> Reference -> Imported Namespace and then add what you would have added on each individual file Import statement and this will cause the import to be added by default - you wont even see the import statement in the code but you'll see the effect.

     

    I'm not sure about the web side stuff, you may be better to ask in the ASP.NET forums   Forums.asp.net for a specific web based usage scenario.

     

    Tuesday, December 04, 2007 9:04 PM

All replies

  • Use the Line

     

    Public Module MyExtensionMethods

     

    That should help you out.

     

    Sunday, December 02, 2007 3:58 PM
  • Hello Spotty,

     

    thank you! That helped me.

     

    I have one more question:

    How can I add my Module "MyExtensionMethods" to the web.config?

    Because I don't want to import it in all the vb files.

     

    I tried this in the web.config but it didn't work:

    <namespaces>

    <clear />

    <add namespace="MyExtensionMethods"/>

    </namespaces>

     

    The error message is:
    A using namespace directive can only be applied to namespaces; 'MyExtensionMethods' is a type not a namespace C:\MyProjects\MyTestApp\web.config 

     

    Thanks for any help.

    Regards

    Joerg

     

     

     

    Sunday, December 02, 2007 7:54 PM
  • Hello,

     

    I have a module with Methods stored in my App_Code Directory as "MyExtensions.vb":

    -----------------------------------------------------------------------------------------------------------

    Public Module MyExtensions

     Public Function IsValidEmailAddress(ByVal email As String) As Boolean

     ....

     End Function

    -----------------------------------------------------------------------------------------------------------

     

    How can I add my Module "MyExtensions" to the web.config?
    Because I don't want to import it in all the vb files.

    I tried this in the web.config but it didn't work:
    <namespaces>
    <clear />
    <add namespace="MyExtensions"/>
    </namespaces>


    The error message is:
    A using namespace directive can only be applied to namespaces; 'MyExtensions' is a type not a namespace C:\MyProjects\MyTestApp\web.config

     

    Thanks in advance for any help.


    Regards

    Joerg

    Monday, December 03, 2007 7:47 PM
  • Your method is contained within a Module

    which both had to be public as the default access I think is freidn which was your first problem.

     

    Extension methods can be defined in namespaces either in code or using the root namespace of the assembly.  However to bring them into scope you would need to import using the Imports statement.

     

    If you want this applicable to all your code so you dont have to specify the imports all the time - you can use a project level imports on your project.  Project properties -> Reference -> Imported Namespace and then add what you would have added on each individual file Import statement and this will cause the import to be added by default - you wont even see the import statement in the code but you'll see the effect.

     

    I'm not sure about the web side stuff, you may be better to ask in the ASP.NET forums   Forums.asp.net for a specific web based usage scenario.

     

    Tuesday, December 04, 2007 9:04 PM