different types of Controls as a parameter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
}
 
Remove the "ref" from the method signature and method invocation, and
you should be fine.
 
I did this and it worked:

Control c=new Control();
c=(Control)myPanel;
SetNewY(ref c, cAbove); //causes compile error
 
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?
 
Back
Top