[ACCEPTED]-VB.NET: How to reference VB.NET module?-vb.net

Accepted answer
Score: 55

You need to mark the module as Public Module.

0

Score: 9

I can't reference to this module but if 17 i put my code in a class. I can reference 16 to it without any problem. Does anyone know 15 why?

Because Modules in VB aren't classes 14 and can't be used to instantiate objects. Rather, they're 13 something similar to namespaces, with the 12 difference that namespaces can't contain 11 functions directly. So the reason for modules 10 is to provide a way to group functions logically 9 that don't belong to a class.

This makes 8 a lot of sense when you consider that not 7 everything logically belongs to a class. Consider 6 System.Math. There is absolutely no reason to make 5 that a class, other than a weird OOP purism.

By 4 the way, you can't reference static classes 3 in C# either, at least not if I understand 2 correctly what you mean by “reference”. Perhaps 1 you can clarify this.

Score: 8

.NET compilers can take any type of language 18 syntax and turn it into a .NET equivalent. Sometimes 17 there is a one for one correspondence other 16 times there isn't.

By using the .NET Reflector you can 15 see what the compiler is really doing.

In 14 VB.NET the module exists because of the heritage 13 inherited from Visual BASIC and partly from 12 Microsoft BASIC.

The VB.NET compiler will 11 take this

Public Module CoreModule
    Dim R As New System.Random(CInt(Microsoft.VisualBasic.Timer))
    Public Function D(ByVal Roll As Integer) As Integer
        Return R.Next(0, Roll) + 1
    End Function

    Public Function _1D6() As Integer
        Return D(6)
    End Function

    Public Function _2D6() As Integer
        Return D(6) + D(6)
    End Function

    Public Function _3D6() As Integer
        Return D(6) + D(6) + D(6)
    End Function

    Public Function _4D6() As Integer
        Return D(6) + D(6) + D(6) + D(6)
    End Function

    Public Function CRLF() As String
        Return Microsoft.VisualBasic.ControlChars.CrLf
    End Function
End Module

And turn it into this (code left 10 out for brevity)

Public NotInheritable Class CoreModule
    ' Methods
    Shared Sub New()
    Public Shared Function _1D6() As Integer
    Public Shared Function _2D6() As Integer
    Public Shared Function _3D6() As Integer
    Public Shared Function _4D6() As Integer
    Public Shared Function CRLF() As String
    Public Shared Function D(ByVal Roll As Integer) As Integer

    ' Fields
    Private Shared R As Random
End Class

In C# the equivalent is 9 this

public sealed class CoreModule
{
    // Fields
    private static Random R;

    // Methods
    static CoreModule();
    public static int _1D6();
    public static int _2D6();
    public static int _3D6();
    public static int _4D6();
    public static string CRLF();
    public static int D(int Roll);
}

All that matter is that the emitted 8 CIL does the job correctly.

This capability 7 is the main reason why so many older Visual 6 BASIC 6 programmers are highly annoyed at 5 MS's changes to the language. For example 4 the keyword Integer emitting a Int32 instead 3 of a Int16.

Modules are exposed to other 2 assemblies referencing the original assembly 1 as long as the module is declared public.

Score: 1

Maybe the methods/subs aren't public? I 4 had that problem once, and it would allow 3 access local code in your class, but not 2 if it was outside your class and marked 1 "Private".

Score: 0
Imports System.Web
Imports System.Web.UI

Module ResponseHelper

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Redirect(ByVal response As Net.HttpWebResponse, _
                        ByVal url As String, ByVal target As String, _
                        ByVal windowFeatures As String)

        If String.IsNullOrEmpty(target) Or _
           target.Equals("_self", StringComparison.OrdinalIgnoreCase) And _
           String.IsNullOrEmpty(windowFeatures) Then
            response.Redirect(url, target, windowFeatures)
        Else
            Dim page As Page = CType(HttpContext.Current.Handler, Page)
            If page Is Nothing Then
                Throw New InvalidOperationException("Cannot redirect to new window outside Page context.")
            End If
            url = page.ResolveClientUrl(url)
            Dim script As String
            If String.IsNullOrEmpty(windowFeatures) Then
                script = "window.open(""{0}"", ""{1}"", ""{2}"";"
            Else
                script = "window.open(""{0}"", ""{1}"");"
            End If
            script = String.Format(script, url, target, windowFeatures)
            ScriptManager.RegisterStartupScript(page, GetType(Page), "Redirect", script, True)

        End If
    End Sub

End Module

0

More Related questions