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 9867
Unless this is a simplified version of what you want, why not just pass the
whole class by reference?
<tg*******@gmai l.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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.co m
<tg*******@gmai l.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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.lef t, sc.otherrect.le ft, sc.anotherrect. left, ..
);
foo( sc.somerect.rig ht, sc.otherrect.ri ght, 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 - debugging
then requires a whole new mindset. I've a cms written in procedural
style that I've been converting to OOP. It is important to always pass
objects by reference. Unthinkingly, writing the constructors, I also
passed my config array by reference....
|
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 outcome between:
function add_some_extra(&$string) {
$string .= 'and something extra.';
}
|
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 in my variables ??
note1: I'm not the one who wrote the web service.. so I can't modify
|
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 defined in
"A.html", but every attempt results in an "is not a function" error. I
have tried to invoke the function using parent.document.funcname(),
top.document.funcname(), and various other identifying methods, but all
result in the above...
|
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, each
requiring a call to that method to fulfill their purpose.
There could be 200, there could be more than 1000.
That is a lot of references passed around. It feels heavy.
Let us say i changed the signature of the interface method to:
| |
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 respond.
I tried calling similar dll for a function which take integer by
refernce. It works ok in this case.
Please can you let me know if we can call a c++ win32 dll function by
passing a xmldocument by reference?
|
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 them I just
set their datasource. I am guessing this is probably what is causing the
problem. Is there a better way to do this?
Anyway this all works happily and things show up when the record already
exists but I have 2 problems ;
1) When I add...
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |