2

I converted some VB Code to C# . Now I am stuck up at this point where I have to pass the parameters using ref keyword. The function is

CurrentZone.Radius = ModSoftUniversal.perirad(ref  (Z2 - Z1), ref ( Xval - Xinc - CurrentZone.Centerx), ref ( dXval - CurrentZone.Centerx), ref ErrorFlag);

The parameters in the function are doing some sott of calculation and to pass the values to the function , ref has to be used. Visual Studio shows the following enter image description here. I need help in passing the parameters

Apoorv
  • 2,023
  • 1
  • 19
  • 42
  • 2
    What would it even mean to pass the *result* of a calculation by reference? (Additionally, please post code *as text* rather than as screenshots - you can then post the error message as further text.) – Jon Skeet May 17 '16 at 05:36
  • 1
    declare the variables, do not do computation as parameters since it is reference parameters it needs a storage location for the value. ex. int minusZ = Z2 - Z1; then change do ref minusZ instead of (Z2 - Z1). – Dr. Stitch May 17 '16 at 05:36
  • Couple of similar links: http://stackoverflow.com/questions/24828842/a-ref-or-out-argument-must-be-an-assignable-variable http://stackoverflow.com/questions/7307705/help-fixing-errors-in-c-sharp-a-ref-or-out-argument-must-be-an-assignable http://stackoverflow.com/questions/2165892/passing-an-explicit-cast-as-a-ref-parameter-c – Mrinal Kamboj May 17 '16 at 05:43

3 Answers3

6

You can't pass expression by reference. Add temporary variable like this:

var z = Z2 - Z1;
var x1 = Xval - Xinc - CurrentZone.Centerx;
var x2 = dXval - CurrentZone.Centerx;
CurrentZone.Radius = ModSoftUniversal.perirad(ref z, ref x1, ref x2, ref ErrorFlag);
CodeFuller
  • 30,317
  • 3
  • 63
  • 79
  • I'd like to point out that attempting to pass `ref (Z2 - Z1)` may indicate that OP is misunderstanding what `ref` is doing. Adding a temporary may be hiding/throwing away an important calculation. `ref` doesn't make sense if the values can simply be thrown away. – Rob May 17 '16 at 05:43
  • Correct. But sometimes signature of the method requires value passed by reference, but result of modifications (if any) is not important for the caller. I believe author knows what he's doing and this is the case. – CodeFuller May 17 '16 at 05:46
  • i knew this approach you mentioned but thought if it could work like this – Apoorv May 17 '16 at 05:50
2

Make the code like this

var z1z2Result = (Z2 - Z1);
var xValxIncres = (Xval - Xinc - CurrentZone.Centerx);
var dxValResult = (dXval - CurrentZone.Centerx);

ModSoftUniversal.perirad(ref  z1z2Result, ref xValxIncres, ref dxValResult, ref ErrorFlag);

The reason is you can't make something byreference if it isn't assigned to a variable it can reference itself to

DevEstacion
  • 1,947
  • 1
  • 14
  • 28
2

You cannot pass a "expression" as ref or out parameter.

In C# ref parameter means that your parameter "reference" will be changed inside the function, and that's why you cannot pass a "expression".

You can read more about it, at https://msdn.microsoft.com/en-AU/library/14akc2c7.aspx

So, your code should look like that:

int Z3 = (Z2 - Z1);
int X1 = ( Xval - Xinc - CurrentZone.Centerx);
int X2 = ( dXval - CurrentZone.Centerx);
bool ErrorFlag = false;
CurrentZone.Radius = ModSoftUniversal.perirad(ref Z3, ref X1, ref X2, ref ErrorFlag);
Paulo Prestes
  • 484
  • 2
  • 8