VB.NET中与C#的using指令相当的是什么?

7

我正在将一些代码从C#转换为VB.NET,并且我需要知道相当于C#的using指令的是什么。

更新:对不起,到目前为止我还没有得到我的答案。这里是一个C#例子:

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

namespace ReportGen
{
    class Reports

3
重点是使用“指令”而不是使用“语句”。 - Hans Passant
5
这就是为什么几乎每个问题都应该包括一段代码片段的原因 :-) - Cody Gray
1
...以及为什么你应该注意术语。你的问题越精确,你得到相关答案的可能性就越大。 - Jon Skeet
4
公平地说,如果提问者了解指令与语句的细微差别,他们可能知道如何自行查找答案。 - Mark Hurd
如果你和我一样,因为寻找类似于 (var obj = new obj) { } 的 using 语句的 VB 等效语而来到这里,那么答案在这里:https://dev59.com/f3NA5IYBdhLWcg3wn_bD。 - Martin Brown
4个回答

12

你正在寻找 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

11

这里有一个链接,展示了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

或者是导入语句(来自 site):

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

-1

使用时,U要大写


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接