Home All Groups Group Topic Archive Search About

different types of Controls as a parameter

Author
25 Mar 2005 9:45 PM
Craig
I want my method to be able to take any control and manipulate the
control....for example change the Location of the control.   I get error
messages when I compile saying for example "Can't convert from ref Label to
ref Control" or "Can't convert from ref Panel to ref Control".

private void SetNewY(ref Control me, ArrayList controlsAboveMe) {
int bfr=7;
int ySize=0;
foreach (Control o in controlsAboveMe) {
  ySize=o.Location.Y + o.Size.Height + bfr;
}
me.Location = new Point(me.Location.X, ySize);
}

private void ChangeControlLocations() {
ArrayList cAbove=new ArrayList();
cAbove.Add(calendar);
cAbove.Add(okBt);
SetNewY(ref myPanel, cAbove); //causes compile error
}

Author
25 Mar 2005 9:57 PM
Mohammad
Remove the "ref" from the method signature and method invocation, and
you should be fine.
Author
25 Mar 2005 9:59 PM
Craig
I did this and it worked:

Control c=new Control();
c=(Control)myPanel;
SetNewY(ref c, cAbove); //causes compile error



Show quote
"Craig" wrote:

> I want my method to be able to take any control and manipulate the
> control....for example change the Location of the control.   I get error
> messages when I compile saying for example "Can't convert from ref Label to
> ref Control" or "Can't convert from ref Panel to ref Control".
>
> private void SetNewY(ref Control me, ArrayList controlsAboveMe) {
> int bfr=7;
> int ySize=0;
> foreach (Control o in controlsAboveMe) {
>   ySize=o.Location.Y + o.Size.Height + bfr;
> }
> me.Location = new Point(me.Location.X, ySize);
> }
>
> private void ChangeControlLocations() {
>  ArrayList cAbove=new ArrayList();
>  cAbove.Add(calendar);
>  cAbove.Add(okBt);
>  SetNewY(ref myPanel, cAbove); //causes compile error
> }
Author
25 Mar 2005 10:13 PM
Mohammad
Yes, that would work as well. But you really don't need the "ref"
modifier at all, since in your method you are not changing the
reference, but rather changing the object that is being referneced in
your method.

Perhaps you're confusing the semnatics of "ByRef" from Visual Basic
with those of "ref" in C#. Let me assure you that they are very
differnt.

Could you try removing the "ref" modifier and the cast to "Control" and
see if it would work?

AddThis Social Bookmark Button