473,385 Members | 1,907 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,385 software developers and data experts.

passing indexers and properties by reference

i'd like to do the following, but i don't think it's possible. can you
help me find a way to do this, or maybe a better way to write the code?
I have a list of items that need to be modified based on their data and
some other data as well. i planned on passing it by reference into a
function that modified their values. but it gave me the error "you
cannot pass indexers and properties by reference". The only way i know
of doing it is making temporary variables for the call, which is quite
cumbersome. code follows for clarity.

Thanks in advance!

actual error message:
error code: CS0206
"A property or indexer may not be passed as an out or ref parameter"

pseudocode:
class myclass
{
public class subclass
{
int i;
int j;
int k;
}

public bool foo(ref int first, ref int second, ref int third)
{
first = second * third;
}

public void start()
{
List<subclass> items = new List<subclass>();
//fill list with items
foreach( subclass sc in items )
{
// this line causes the error.
// cannot pass properties by reference
foo(ref sc.i, ref sc.j, ref sc.k);

// this, however, works
int tempi, tempj, tempk;
tempi = sc.i;
tempj = sc.j;
tempk = sc.k;
foo( ref tempi, ref tempj, ref tempk );
sc.i = tempi;
sc.j = tempj;
sc.k = tempk;
}
}
}

Dec 1 '05 #1
4 9846
Unless this is a simplified version of what you want, why not just pass the
whole class by reference?

<tg*******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
i'd like to do the following, but i don't think it's possible. can you
help me find a way to do this, or maybe a better way to write the code?
I have a list of items that need to be modified based on their data and
some other data as well. i planned on passing it by reference into a
function that modified their values. but it gave me the error "you
cannot pass indexers and properties by reference". The only way i know
of doing it is making temporary variables for the call, which is quite
cumbersome. code follows for clarity.

Thanks in advance!

actual error message:
error code: CS0206
"A property or indexer may not be passed as an out or ref parameter"

pseudocode:
class myclass
{
public class subclass
{
int i;
int j;
int k;
}

public bool foo(ref int first, ref int second, ref int third)
{
first = second * third;
}

public void start()
{
List<subclass> items = new List<subclass>();
//fill list with items
foreach( subclass sc in items )
{
// this line causes the error.
// cannot pass properties by reference
foo(ref sc.i, ref sc.j, ref sc.k);

// this, however, works
int tempi, tempj, tempk;
tempi = sc.i;
tempj = sc.j;
tempk = sc.k;
foo( ref tempi, ref tempj, ref tempk );
sc.i = tempi;
sc.j = tempj;
sc.k = tempk;
}
}
}

Dec 1 '05 #2
This is right, you can't pass a property/indexer by reference, since
properties and indexers represent actual methods.

What you need to do is get the value of the property/indexer, call your
method, changing the value, and then set the value back.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<tg*******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
i'd like to do the following, but i don't think it's possible. can you
help me find a way to do this, or maybe a better way to write the code?
I have a list of items that need to be modified based on their data and
some other data as well. i planned on passing it by reference into a
function that modified their values. but it gave me the error "you
cannot pass indexers and properties by reference". The only way i know
of doing it is making temporary variables for the call, which is quite
cumbersome. code follows for clarity.

Thanks in advance!

actual error message:
error code: CS0206
"A property or indexer may not be passed as an out or ref parameter"

pseudocode:
class myclass
{
public class subclass
{
int i;
int j;
int k;
}

public bool foo(ref int first, ref int second, ref int third)
{
first = second * third;
}

public void start()
{
List<subclass> items = new List<subclass>();
//fill list with items
foreach( subclass sc in items )
{
// this line causes the error.
// cannot pass properties by reference
foo(ref sc.i, ref sc.j, ref sc.k);

// this, however, works
int tempi, tempj, tempk;
tempi = sc.i;
tempj = sc.j;
tempk = sc.k;
foo( ref tempi, ref tempj, ref tempk );
sc.i = tempi;
sc.j = tempj;
sc.k = tempk;
}
}
}

Dec 1 '05 #3
thanks for the reply...

and yes, it is a simplified version of what i want. the "subclass" i'm
using has a bunch of rectangles for drawing, and i call "foo" 4 times,
one for each point in the rectangle (left, top, bottom, right). so it
kinda looks like this in my code:

foreach( subclass sc in items )
{
foo( sc.somerect.left, sc.otherrect.left, sc.anotherrect.left, ..
);
foo( sc.somerect.right, sc.otherrect.right, sc.anotherrect.right,
.... )
...
}

i would like to abstract out the "left" "right" etc out of foo() so
that i can just call it multiple times.

but reading "nicholas paldino's" response below i think i can come up
with a solution. thanks!

Dec 1 '05 #4
i think i understand now, i didn't know the definition of "properties"
i guess. i thought properties also referred to just plain member
variables (without a get/set), but i see now that is not the case. And
it at least makes sense that they might not allow you to pass the
property set() function by reference because it doesn't match the ref
int type.

Thanks!

Dec 1 '05 #5

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

Similar topics

0
by: lawrence | last post by:
Those of you with backgrounds with the C language will laugh at my mistake, but those of you, like myself, who deal mostly with PHP should be warned about passing variables as references -...
4
by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in...
4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
4
by: saish | last post by:
Hello Need your help, I have a C++ win32 dll which takes xml document type by reference. I need to call this dll from c# .net. I tried using DllImport, but dll funtion when call does not...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
4
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
8
by: Ariel White | last post by:
What does it mean to pass data by value and how does that compare to passing data by reference? Please explain to me why we would pass some data by value and others by reference.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.