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;
}
}
} 4 9811
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; } } }
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; } } }
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!
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 -...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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);
|
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...
|
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.
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |