passing by ref for value type and storing this in a member...

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
 
B

Bruce Wood

Essentially what you want is a pointer to the value type, but in the
case of a value type I'm not sure what that means. I'll explain. Before
I do, though, have you noticed that your code doesn't do what you'd
like it to?

class A
{
bool refval;

A(ref bool val)
{
refval = val;
}

B()
{
refval = true;
}
}

In this class, the line

reval = val;

just copies the value of the bool from "val" into "refval". It doesn't
_store a pointer_ to the value that was passed into A(). So, when you
call B(), all it does is update the "refval" bool to contain something
different. This is _not_ the same as the old C way of doing it:

int *refvalp;

void A(int *valp)
{
refvalp = valp;
}

void B(void)
{
*refvalp = true;
}

because in the C case you're holding a _pointer_ to an int, whereas in
the C# case you're holding a bool, not a pointer (or a reference), and
there's no way to declare a class member like this:

private bool ref refval;

....in other words, in C# there's no way to declare a pointer.

Think of what would happen if you could. Let's say that your class does
what you would like: it stores a "pointer" to an int and then allows
you to update it later. What if your class instance outlives the int?
Consider this:

public void CrashAndBurn()
{
A = GetA();
UseA(A);
}

public A GetA()
{
bool x;
return new A(ref x);
}

public void UseA(A anA)
{
int i;
anA.B();
}

The line anA.B() would be following the "pointer" it holds and updating
the bool "x", but "x" is a local variable of the method GetA(), which
has finished and released its stack space... stack space that is now
being used by UseA to hold its int, i. So, calling anA.B() would slam a
bool onto the stack where "x" _was_, and in so doing corrupt "i".
Instant stack corruption. Easy end run around all of the security that
..NET offers.

That's why C# doesn't allow pointers in safe code.

Now, that said, this doesn't mean that there aren't ways to hold
references to particular properties of particular instances allocated
on the heap. You can do that using Reflection. You cannot, however,
hold a reference to an arbitrary value type and then update it whenever
you like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top