Domanda

When creating a With statement like

With A
   .Method1OfA
   .Method2OfA
   .Method3OfA
End With

is there a way to reference the currently "With"ed variable within the With statement block?

Since I don´t know of any, I find myself writing stuff like this:

With A
   .Method1OfA
   GlobalFunction (A)
   .Method2OfA
   GlobalFunction (A)
   .Method3OfA
End With

i.e. I am dereferencing A more often than I´d like. If I could write something like

With A
   .Method1
   GlobalFunction (currentlyWithed)
   .Method2
   GlobalFunction (currentlyWithed)
   .Method2
End With

with currentlyWithed "automatically" referring to A, I could avoid that dereference, which I want to, because if A is a complex term (which it probably is, being the reason for the With statement in the first place), I´d avoid multiple evaluations of that term.

The workaround solution a la

Dim B: Set B=A
With B
   .Method1
   GlobalFunction (B)
   .Method2
   GlobalFunction (B)
   .Method2
End With

is acceptable, but creates a temporary variable (B) which survives the scope of the With statement, and referencing it later by accident might create problems if A has changed meanwhile (i.e. if it has been modified meanwhile and references a different instance).

So I would expect there is something like currentlyWithed in VB.NET or VBScript, but i fail to find anything like that in the documentation.

È stato utile?

Soluzione

Just do your workaround but in its own method. Although it creates a temporary object, it will go out of scope at the end of the method (meaning you don't have to worry about object not being cleaned up):

Public Sub DoSomething(a As Object)
    Dim B: Set B=a
    With B
       .Method1
       GlobalFunction (B)
       .Method2
       GlobalFunction (B)
       .Method2
    End With
End Sub
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top