0

I am using the SyntaxWalker to go through a tree and in the function below I realize I need to insert a statement before the one I am working on.

Public Overrides Function VisitDeclarationExpression(ByVal Node As CSS.DeclarationExpressionSyntax) As VisualBasicSyntaxNode
    Dim VariableDesignation As CSS.SingleVariableDesignationSyntax = CType(Node.Designation, CSS.SingleVariableDesignationSyntax)
    Dim name As NameColonEqualsSyntax = Nothing
    Dim value As ExpressionSyntax = CType(VariableDesignation.Accept(Me), ExpressionSyntax)
    ' Need to Insert Dim before the statement that contains this SimpleArgument 
    Return SyntaxFactory.SimpleArgument(name, value)
End Function

I need to declare "name = Nothing" so I can use name in a TryGetValue(name). In C# it would be easy using

return v.TryGetValue(out var name)

but in VB I need two statements

Dim name As Object = Nothing
Return v.TryGetValue(name)

How do I insert the Dim above the statement I am in the middle of walking?

Paul Cohen
  • 241
  • 4
  • 14
  • I'm not sure if I understood your problem right, but...VB automatically initialises all local variables in a method, so you can use ByRef without needing to explicitly initialise the variable first. Take a look here: https://stackoverflow.com/a/4358774/5688800 – Rafa Gomez Jan 14 '18 at 03:09
  • The variable doesn't exist (the statement is only the second line shown in the example), I need to create it and until some future release of VB you get a warning about use of uninitialized variable even thought the function being called declares the parameter as – Paul Cohen Jan 14 '18 at 05:45

0 Answers0