Search code examples
vb.netsyntax-errorusing-statement

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.


Solution

  • 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