D
Dave Parry
Hi,
I would like to hear peoples thoughts on passing a value type by reference
and then trying to store this ref in a member so it can be updated
elsewhere, whereby I want the referenced value to be updated.
eg.
class A
{
bool refval;
A(ref bool val)
{
refval = val;
}
B()
{
refval = true;
}
}
main()
{
bool passbyref = false;
A a = new A(ref passbyref);
// value of passbyref this point is still false.
a.B();
// I want value of passbyref at this point to be true, however it is
still false as class A simply took a copy of ref.
}
In a nutshell I want the 'passbyref' reference to be updated in A.B()
method.
I'd like to do this without resorting to other methods such as passing a
struct (non value type), object etc.
Or is this prevented because it might be considered as bad design?
Any help would be appreciated.
Thanks,
Dave
I would like to hear peoples thoughts on passing a value type by reference
and then trying to store this ref in a member so it can be updated
elsewhere, whereby I want the referenced value to be updated.
eg.
class A
{
bool refval;
A(ref bool val)
{
refval = val;
}
B()
{
refval = true;
}
}
main()
{
bool passbyref = false;
A a = new A(ref passbyref);
// value of passbyref this point is still false.
a.B();
// I want value of passbyref at this point to be true, however it is
still false as class A simply took a copy of ref.
}
In a nutshell I want the 'passbyref' reference to be updated in A.B()
method.
I'd like to do this without resorting to other methods such as passing a
struct (non value type), object etc.
Or is this prevented because it might be considered as bad design?
Any help would be appreciated.
Thanks,
Dave