The With statement is used to take an object variable and to have that easily available.
Take for example the following example:
pContext.WebForm.DataSet.Fields("Name").Value = "Test"
pContext.WebForm.DataSet.Fields("Address").Value = "MyStreet"
pContext.WebForm.DataSet.Fields("EmailAddress").Value = "test@test.com"
'This can be simplyfied with a With-block:
With pContext.WebForm.DataSet
.Fields("Name").Value = "Test"
.Fields("Address").Value = "MyStreet"
.Fields("EmailAddress").Value = "test@test.com"
End With
The Using block is very much alike to the With-block. However, there is one major and important difference: a using block will (always!) destruct / dispose the object it is using when the block ends. Even if the function is exited because of an error or a Return statement, still the referred object is destructed/disposed.
This feature is very important for Polar Studio. It allows you to easily use temporary resources - like DataSets or Recordsets - that are disposed after use, freeing resources, database locks etc.
Using pContext.OpenDynamicRecordSet(@"SELECT * FROM Customer WHERE CustomerID = {pCustomerID}")
If Not .Eof Then
lName = .Fields("Name").Value
lEmailAddress = .Fields("EmailAddress").Value
End If
End Using
Warning: if you use the Using block on a variable you did not create yourself, you may end up destructing objects you did not want to destruct. In that case you should use a With block.
You can access the object of the With/Using block itself by simply using a single dot, as in:
Function CreateCustomersReport(pContext)
Using pContext.OpenDynamicRecordSet("SELECT * FROM Customer")
Return CreateReportFromRecordSet(pContext, .) 'Here the dot will pass the current recordset as parameter to the function CreateReportFromRecordSet
End Using
End Function