473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

different types of Controls as a parameter

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
}
Nov 17 '05 #1
6 1094
Remove the "ref" from the method signature and method invocation, and
you should be fine.

Nov 17 '05 #2
I did this and it worked:

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

"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
}

Nov 17 '05 #3
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?

Nov 17 '05 #4
Remove the "ref" from the method signature and method invocation, and
you should be fine.

Nov 17 '05 #5
I did this and it worked:

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

"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
}

Nov 17 '05 #6
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?

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ryan Taylor | last post by:
Hello. I am trying to overload a method so that I have four possible working copies. The only difference between the four methods is what to search by and in what manner to return the results....
10
by: Arno R | last post by:
Hi all, I have a database that I need to use in different versions of Access. This is A97 in most places and A2k in a few other locations. (I develop in A97 and convert the db to A2k for these...
6
by: NutsAboutVB | last post by:
Hello, I am a .NET programmer and I have a JPEG image file (from digital camera) of about 109 KB's in size, when I open it and save it (without making any alterations at all, just going to File...
2
by: mast2as | last post by:
Hi there, for a long time I've been trying to think of way of saving different data of different types using one single class (well 2 in reality, a class for the data, and 1 class for a list of...
1
by: Senthryl | last post by:
I just moved from Visual Basic to C++ and recently finished reading my book. However, I'm trying to implement something which has led me to writing the same thing many times, which I've learned is...
14
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef...
2
by: forest demon | last post by:
i'm trying to create a custom control that contains multiple controls of different types(textbox, combox, checkbox, etc..) the examples i see derive from one type....for instance TextBox....
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
3
by: PLS | last post by:
VB.NET allows defining a property that has different get and set types by doing this Public Property fFlag(ByVal flag as flagEnumeration) As Boolean Get Return ... End Get Set(ByBal Value as...
24
by: NaftoliGug | last post by:
Is there a way to have unbound, uncalculated controls (or a calculated control that is based on a function) that retains a different value for each record?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.