Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using statement in Visual Basic

I'm having difficultly with the using statement in Visual Basic. Does anyone know how this should be written?:

Using (Dim doc As WordprocessingDocument = WordprocessingDocument.Open(filename, True))
//blah blah
End Using

The code works fine without the using and its obviously as syntactic error. "Dim" is highlighted, and an expression is expected apparently. Sorry if this a bit basic, but the info on vb using statements is not clear and its obviously doesn't work in the c# style.

like image 229
Nathan Cooper Avatar asked Feb 04 '14 14:02

Nathan Cooper


People also ask

What is statement in Visual Basic?

A statement in Visual Basic is a complete instruction. It can contain keywords, operators, variables, constants, and expressions. Each statement belongs to one of the following categories: Declaration Statements, which name a variable, constant, or procedure, and can also specify a data type.

What is using () in C#?

The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned. A variable declared with a using declaration is read-only.

What is conditional statement in Visual Basic?

Conditional statements are used to perform different actions for different decisions. In VBScript we have four conditional statements: If statement - executes a set of code when a condition is true. If...Then... Else statement - select one of two sets of lines to execute.

How are the with and end with statements used?

By using With... End With , you can perform a series of statements on a specified object without specifying the name of the object multiple times. Within a With statement block, you can specify a member of the object starting with a period, as if the With statement object preceded it.


1 Answers

There's two things wrong.

First, you must remove the Dim keyword. The Using keyword replaces the Dim keyword. Both Dim and Using have the same effect of declaring a new variable, just in different ways.

Secondly, you must remove parentheses. The very first thing after the Using keyword must be the variable name.

Using doc As WordprocessingDocument = WordprocessingDocument.Open(filename, True)
    ' blah blah
End Using
like image 111
Steven Doggart Avatar answered Sep 29 '22 03:09

Steven Doggart