Error passing a reference to a derived object in a method

In C #, I'm trying to implement a method that I can use to bind data to any control that I pass to it (unless, of course, the control is obtained from the object associated with the databoundcontrol)

subject to the method

 public void CTLBindData(ref DataBoundControl ctl){ ... }

I get an error when trying to pass a derived control to a function like the following code

DropDownList lister = new DropDownList();  
CTLBindData(ref lister);

Creates a conversion error

Ok, I can accept this, but the following bothers me (perhaps because I'm used to C ++ not C #)

CTLBindData(ref (DataBoundControl)lister);

in this case, I get the error "The argument ref or out must be an assignable variable"

For clarification. Drop-down list inherits a list control that inherits from DataBoundControl

, , . , .

, ?

DC

+3
2

:

DataBoundControl countrol = (DataBoundControl)lister;
CTLBindData(ref control);

# , ref ( ), . , , .

. ref out ? :

, "X", X -, X. , , X. , "ref X", ref X, . ? , , ref?

+6

, ref. # *. ( , , ref.) , , . ref .

** , , , #. . , . *

0

Source: https://habr.com/ru/post/1721755/


All Articles