【问题标题】:VB.NET equivalent to C#'s using directiveVB.NET 等效于 C# 的 using 指令
【发布时间】:2025-12-24 08:55:11
【问题描述】:

我正在将一些代码从 C# 转换为 VB.NET,我需要知道 C# 的 using 指令的等价物是什么。

更新:抱歉,到目前为止我还没有得到答案。这是一个 C# 示例:

using moOutlook = Microsoft.Office.Interop.Outlook;
using moExcel = Microsoft.Office.Interop.Excel;

namespace ReportGen
{
    class Reports

【问题讨论】:

  • 这是 using directive 而不是 using statement
  • 这就是为什么几乎每个问题都应该包含一个代码 sn-p :-)
  • ...以及为什么要注意术语。您的问题越准确,您获得相关答案的可能性就越大。
  • @JonSkeet 但是,公平地说,如果 OP 知道指令与语句的细微差别,他们可能知道如何自己找到答案。
  • 如果你喜欢我,你最终会在这里寻找语句,即 using (var obj = new obj) { } 等价的答案在这里*.com/questions/887831/…

标签: c# vb.net c#-to-vb.net


【解决方案1】:

这里是 C# 和 VB.NET 语法比较的链接。

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

来自链接:

Using reader As StreamReader = File.OpenText("test.txt")
  Dim line As String = reader.ReadLine()
  While Not line Is Nothing
    Console.WriteLine(line)
    line = reader.ReadLine()
  End While
End Using

或进口声明(也来自网站):

Imports System 

Namespace Hello
   Class HelloWorld 
      Overloads Shared Sub Main(ByVal args() As String) 
         Dim name As String = "VB.NET" 

         'See if an argument was passed from the command line
          If args.Length = 1 Then name = args(0) 

          Console.WriteLine("Hello, " & name & "!") 
      End Sub 
   End Class 
End Namespace

【讨论】:

    【解决方案2】:

    “使用”大写字母 U

    【讨论】:

      【解决方案3】:
      Imports moOutlook = Microsoft.Office.Interop.Outlook; 
      Imports moExcel = Microsoft.Office.Interop.Excel;
      

      见:Global Import/using Aliasing in .NET

      【讨论】:

        【解决方案4】:

        您正在寻找Imports 声明。将您需要的任何导入语句放在代码文件的最顶部,就像 C# 中的 using 指令一样:

        Imports moOutlook = Microsoft.Office.Interop.Outlook
        Imports moExcel = Microsoft.Office.Interop.Excel
        
        Namespace ReportGen
           Public Class Reports
              'Your code here
           End Class
        End Namespace
        

        【讨论】: