ref assignment

  • Thread starter Thread starter MRe
  • Start date Start date
M

MRe

Hi,

Why doesn't this work (it gives the error 'A ref or out argument must be
an assignable variable').

public void Caller()
{
int Value;
Callee(ref Value = 5); // <-- Error here under Value = 5; it's
assignable, look at me assign, woo!
}
public void Callee(ref int Value)
{
MessageBox.Show(Value.ToString());
}

..is ref evaluated before any operators (It also doesn't work with
Callee(ref (Value = 5));)?

I'm using VS2005; .NET2.0.

Thank you,
Eliott
 
Elliot,

I would think that 5 is not assignable.

If anything, even if this syntax is legal (which I don't think it is),
it isn't very clean. I would avoid it.
 
The result of Value = 5 is not a reference to 'Value' it is a copy of
'Value' and that copy cannot be passed by reference.

Contrast this with this method
public void Callee(int value)
{
}
with this signature 'Callee(Value=5)' will succeed because a copy of an int
is expected.
 
Thank you for the reply Noah,

| The result of Value = 5 is not a reference to 'Value' it is a copy of
| 'Value' and that copy cannot be passed by reference.

Does that mean that every time an assignment is made to a simple type, it
does the assignment to the variable on the stack (does C# use a stack?), and
then, also creates a new copy to return as a result of the operator?

Thanks again,
Eliott


| The result of Value = 5 is not a reference to 'Value' it is a copy of
| 'Value' and that copy cannot be passed by reference.
|
| Contrast this with this method
| public void Callee(int value)
| {
| }
| with this signature 'Callee(Value=5)' will succeed because a copy of an
int
| is expected.
|
|
| "MRe" <mreatdublindotie> wrote in message
| | > Hi,
| >
| > Why doesn't this work (it gives the error 'A ref or out argument must
be
| > an assignable variable').
| >
| > public void Caller()
| > {
| > int Value;
| > Callee(ref Value = 5); // <-- Error here under Value = 5; it's
| > assignable, look at me assign, woo!
| > }
| > public void Callee(ref int Value)
| > {
| > MessageBox.Show(Value.ToString());
| > }
| >
| > ..is ref evaluated before any operators (It also doesn't work with
| > Callee(ref (Value = 5));)?
| >
| > I'm using VS2005; .NET2.0.
| >
| > Thank you,
| > Eliott
| >
| >
|
|
 
C# has value types and reference types. The value types are stack based and
the reference types are heap based with the reference pointer on the stack.
double, byte, char, int32, int64 are value types as are structs constants
and enumerations.

In Callee(Value = 5) 'Value=5' is an expression that assigns 5 to Value and
returns a copy of Value.
int xyz = (Value = 5)
The compiler realizes that it is an expression and not a variable reference.
 
Ah, okay - I was thinking I was doing like c++..

void callee(int* Value);
callee(&(Value = 5));

...buts no, I wasn't.. Hey! Did you say constants go on the stack - that's
crazy! Well, I guess they got to go somewhere. Anyway..

It looked like an annoying peculiarity, but it makes sense now, so I can
live with it.
Thank you very much for you help Noah :)
Eliott
 
Why doesn't this work (it gives the error 'A ref or out argument must be
an assignable variable').

The expression Value=5 is classified as a value (the result of the
assignment) not a variable. Without getting "ref" involved, it's the
same reason you can't do:

int value;

(value = 5) = 10;
 
Back
Top