7

I am converting some code from C# to VB.NET, and I need to know what the equivalent is for C#'s using directive.

Update: Sorry, but so far I haven't gotten my answer. Here is a C# example:

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

namespace ReportGen
{
    class Reports
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
  • 3
    It is the using *directive* not the using *statement*. – Hans Passant Nov 09 '10 at 15:05
  • 5
    This is why almost every question should include a code snippet :-) – Cody Gray - on strike Nov 09 '10 at 15:12
  • 1
    ... and why you should take care with terminology. The more precise you are with your question, the more likely you are to get the relevant answer. – Jon Skeet Nov 09 '10 at 18:33
  • 4
    @JonSkeet but, in fairness, if the OP knew the nuance of directive vs statement, they probably knew how to locate the answer themselves. – Mark Hurd Aug 19 '12 at 00:27
  • If you like me you ended up here looking for the statement ie using (var obj = new obj) { } equivalent the answer is here http://stackoverflow.com/questions/887831/how-does-the-using-statement-translate-from-c-sharp-to-vb – Martin Brown Jul 15 '14 at 16:03

4 Answers4

12

You're looking for the Imports statement. Place any import statements that you need at the very top of your code file, just like the using directive in C#:

Imports moOutlook = Microsoft.Office.Interop.Outlook
Imports moExcel = Microsoft.Office.Interop.Excel

Namespace ReportGen
   Public Class Reports
      'Your code here
   End Class
End Namespace
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
11

Here is a link showing a syntax comparison between C# and VB.NET side by side.

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

From the link:

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

Or the imports statement (from site also):

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
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
2
Imports moOutlook = Microsoft.Office.Interop.Outlook; 
Imports moExcel = Microsoft.Office.Interop.Excel;

see: Global Import/using Aliasing in .NET

Community
  • 1
  • 1
Kell
  • 3,252
  • 20
  • 19
-1

"Using" with a capital U

Henri
  • 5,065
  • 23
  • 24