If you have ever written code to lock variables inside a SSIS Script Task or Script Component then you will probably have written some VB.Net code that looks something like this:
Public Sub Main()
Dim vars As Variables
Dim fireAgain As Boolean = True
Dts.VariableDispenser.LockOneForRead("VarName", vars)
'Do something with the value....
vars.Unlock()
Dts.TaskResult = ScriptResults.Success
End Sub
If you have then moved to SSIS2008 then you may want to do similar in C# so you would convert this code to be something like:
public void Main() {
Variables vars;
bool fireAgain = true;
Dts.
VariableDispenser.
LockOneForRead("varName",
ref vars);
//Do something with the value...
vars.
Unlock(); Dts.
TaskResult = (int)ScriptResults.
Success;
}
However, doing so will result in an error:
Use of unassigned local variable 'vars'
What gives? Are you telling me the same logic doesn't work in C#? Actually the solution is pretty simple. Due to the way that C# and VB initialise their variables differently you simply have to set your variable to NULL as follows:
public void Main() {
Variables vars = null ;
bool fireAgain = true;
Dts.
VariableDispenser.
LockOneForRead("varName",
ref vars);
//Do something with the value...
vars.
Unlock(); Dts.
TaskResult = (int)ScriptResults.
Success;
}
Easy when you know how!!!
@Jamiet