If we want to replace an object variable itself (not its property or field), we should mark the argument with “ref”
public void CreateTextBox(ref TextBox txt)
{
if (txt == null)
txt = new TextBox();
}
public void TestCreateControl()
{
TextBox txtEmpID;
CreateTextBox(ref txtEmpID);
CreateTextBox(ref txtEmpID);
}
In this case, we should mark the argument txt with ref, otherwise the TextBox creation will not be reflected in the passed parameter.
In the first call, the txt object will be created and it will reflect in the txtEmpID. In the second call, as the txt is not null, it won’t create the TextBox object.
No comments:
Post a Comment