20

I got a Utility module since VB.NET doesn't have static class like C# and Module is the static class in VB.NET. The reason that I use module is because I'm using the Extension method and it can only be use in Module.

I can't reference to this module but if I put my code in a class. I can reference to it without any problem. What could be the reason? I missed C#.

Edit: The module is inside a class library call Utility.

2
  • Think you misunderstand about VB.Net's capabilities. Modules are not Shared/Static Classes, they are global to the Namespace methods that can be used anywhere. They are usually Type abstract, not Extensions. Functions, Subs, and even Extensions all live out of the OOP bubble, described by @Konrad-Rudolph
    – GoldBishop
    Mar 8, 2017 at 19:24
  • Another point, for future viewers....The names of the Modules must not have the same name between Libraries. The names must be unique in name between the uses.
    – GoldBishop
    Mar 8, 2017 at 19:41

6 Answers 6

55

You need to mark the module as Public Module.

0
9

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

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

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

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

3
  • I've always thought that, but then what's wrong with placing then in a "Shared" class? If a Function takes in data, does a calculation, and spits out results, without using or modifying anything else aside from the input data, why can't it be in a class and marked "Shared"?
    – SqlRyan
    Jan 12, 2009 at 19:21
  • 1
    The question is: why should it? What advantage does the class have? OOP as a paradigm is only useful if it has an advantage. Classes without data (and without place as a marker type) don't serve any purpose. Jan 12, 2009 at 19:26
  • @SqlRyan why should a "generic" even reside with the bubble of a Class? Why not reside in the namespace? or better yet, just in the Library itself. If it has no relevance to the Class or Namespace, why should it be tied down to one? Modules are a Library scoped way of creating Methods without marrying them strictly to an Object that does not properly describe its use.
    – GoldBishop
    Mar 8, 2017 at 19:27
8

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

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

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

The VB.NET compiler will 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 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 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 CIL does the job correctly.

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

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

2
  • Clarification, BasicA was never owned by Microsoft....Microsoft started ownership around QBasic. Basic
    – GoldBishop
    Mar 8, 2017 at 19:29
  • Nice! This helped me convert a module so I could reference its type from a member method to log the class name with Serilog.
    – Daniel P
    Feb 14, 2022 at 12:47
1

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

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
1
  • are you trying to overwrite response.redirect with a javascript function >.<
    – Shawn
    Jan 13, 2009 at 1:22
-1

I don't understand what you are asking.

VB.NET does have static classes, just like in C#, because in VB.NET a Module IS a static class. They are one and the same. Anything you can do with a static class you can do with a Module. Perhaps you haven't marked your Public/Private/Protected access correctly?

1
  • 4
    @Joel: no, they are not the same on a language level. In particular, modules implicitly import their names into the current namespace. This means that you don't have to prefix a module's names with the module name when referring to them (provided the module's surrounding namespace is imported). Jan 12, 2009 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.