pass control as byref function argument

Hi,

Can I pass a contrl as a function argument "by reference" in C#

I want to make changes to that control in another class's function and want it to be reflected in the actual control, without returning any values.
 
for example, in ProjectA ClassA I have a control checkedlistbox. In ProjectB, I have a class ClassB and a function FuncB.

From ClassA of ProjectA, 


objClassB.funcb(checkedlistbox)
{
//change selected item of checkedlistbox etc
//dont want to return anything
}
</Code>
and the changes I am doing in ClassB of ProjectB to checkedlistbox, should be reflected on the formA of ClassA.

Is this possible to do 
if yes, then what is the syntax of function declaration that takes control as reference in C#, and also how would call this function and pass control as reference(syntax) in C#

Thanks,


Answer this question

pass control as byref function argument

  • DanMoseley - MSFT

    Hi,

    Got following error, while trying to do a "Select All" in checkedlistbox items 

    An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll

    Additional information: The list that this enumerator is bound to has been modified. An enumerator can only be used if the list doesn't change.


    heres my code

    public void test(CheckedListBox ctrl)
    {
       foreach(object litem in ctrl.Items)
    {
                      ctrl.SetItemChecked(ctrl.Items.IndexOf(litem),true);
    }
    }

    How to fix 

  • Benjamin R.

    Nata1 is right... sort of.

    To do what you're trying to accomplish, you don't need to pass the argument in any special way.  However, it is important to realize that the variable is not passed by reference automatically, like Nata1 said.

    When you pass a value type (int, double, struct, etc.), you are passing the actual value.  When you pass a reference type (CheckedListBox, ArryList, etc.), you are passing a reference (pointer) to the actual value.  But you are <i>still passing it by value</i>.  If you pass a reference type by reference, you are actually passing a pointer to a pointer.  This is not what you want to do in the majority of cases.

    Here's an example:private void Example()
    {
       ArrayList list = new ArrayList();
       Console.WriteLine(list.Count);
     
       PassByVal(list);
       Console.WriteLine(list.Count);
     
       PassByRef(ref list);
       Console.WriteLine(list.Count);
    }
       
    void PassByVal(ArrayList list)
    {
       list.Add(new object());
       list = null;
    }
     
    void PassByRef(ref ArrayList list)
    {
       list.Add(new object());
       list = null;
    }
    When list is set to null in PassByVal, only the local variable is affected.  The Console.WriteLine(list.Count) statement will still print 1.  However, when list is set to null in PassByRef, the following Console.WriteLine(list.Count) statement will cause a NullReferenceException to be thrown.  This is because PassByRef was dealing with the <i>actual reference</i> that was created in the Example method, not just a copy of it.

    It can be confusing at first, but it's an important distinction.  More often thatn not, passing a reference type by reference is not what you want to do.

  • mndepaul

    Its automatically passed by reference
  • pass control as byref function argument