tags:

views:

4180

answers:

6

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  A: 

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.

Konrad Rudolph
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"?
rwmnau
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.
Konrad Rudolph
(sigh) Good old java.lang.Math...
Rob
+1  A: 

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".

rwmnau
A: 

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?

Joel Coehoorn
@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).
Konrad Rudolph
A: 
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
Jack
are you trying to overwrite response.redirect with a javascript function >.<
Shawn Simon
+2  A: 

Gee, I'm stupid. You need to mark the module as Public Module. Thank everyone.

Jack
+1  A: 

.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.

RS Conley