VB.NET:如何引用 VB.NET 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/436605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:01:15  来源:igfitidea点击:

VB.NET: How to reference VB.NET module?

vb.net

提问by Hyman

I got a Utility module since VB.NETdoesn'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.

我得到了一个 Utility 模块,因为VB.NET没有像 C# 这样的静态类,而 Module 是 VB.NET 中的静态类。我使用module的原因是因为我使用的是Extension方法,它只能在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#.

我不能引用这个模块,但如果我把我的代码放在一个类中。我可以毫无问题地参考它。可能是什么原因?我错过了 C#。

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

编辑:该模块位于类库调用实用程序中。

采纳答案by Konrad Rudolph

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.

因为 VB 中的模块不是类,不能用于实例化对象。相反,它们类似于命名空间,不同之处在于命名空间不能直接包含函数。所以模块的原因是提供一种方法来对不属于类的函数进行逻辑分组。

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.

当您考虑到并非所有逻辑上都属于一个类时,这很有意义。考虑System.Math。除了奇怪的 OOP 纯粹主义之外,绝对没有理由将其作为一个类。

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.

顺便说一下,你也不能在 C# 中引用静态类,至少如果我正确理解你所说的“引用”是什么意思的话。也许你可以澄清这一点。

回答by Hyman

You need to mark the module as Public Module.

您需要将模块标记为公共模块。

回答by RS Conley

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

.NET 编译器可以采用任何类型的语言语法并将其转换为 .NET 等效项。有时有一对一的对应关系,有时则没有。

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

通过使用.NET Reflector,您可以看到编译器真正在做什么。

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

VB.NET 中,模块的存在是因为继承自 Visual BASIC 和部分来自 Microsoft BASIC。

The VB.NET compiler will take this

VB.NET 编译器将采用这个

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

在 C# 中,等效的是这个

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 CILdoes the job correctly.

重要的是发出的CIL正确完成工作。

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.

这种能力是许多较老的 Visual BASIC 6 程序员对 MS 对语言的更改感到非常恼火的主要原因。例如,关键字 Integer 发出 Int32 而不是 Int16。

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

只要模块被声明为 public,模块就会暴露给引用原始程序集的其他程序集。

回答by SqlRyan

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

也许方法/订阅不是公开的?我曾经遇到过这个问题,它允许访问您班级中的本地代码,但如果它在您的班级之外并标记为“私人”,则不允许。

回答by Hyman

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

回答by Joel Coehoorn

I don't understand what you are asking.

我不明白你在问什么。

VB.NET doeshave static classes, just like in C#, because in VB.NET a ModuleISa 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?

VB.NET确实有静态类,就像在 C# 中一样,因为在 VB.NET 中 a ModuleISa static class. 他们是一样的。你可以用静态类做的任何事情,你都可以用模块做。也许您没有正确标记您的公共/私人/受保护访问?