Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass reference to the function

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

like image 430
Apoorv Avatar asked Sep 13 '26 03:09

Apoorv


2 Answers

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);
like image 125
CodeFuller Avatar answered Sep 15 '26 15:09

CodeFuller


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

like image 23
DevEstacion Avatar answered Sep 15 '26 16:09

DevEstacion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!