T
tg.foobar
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;
}
}
}
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;
}
}
}