With the code below I am reading process variables from a PLC controller. The method has outputs like var_result and var_result_float. I get the Error CS0177 in the last line regarding that these output parameters are not assigned.In the second for loop I had assigned them values for all conditions. I cannot find what is wrong.
Error CS0177 The out parameter 'var_result' must be assigned to before control leaves the current method...
Error CS0177 The out parameter 'var_result_float' must be assigned to before control leaves the current method...
public Int32 doReadMixEx2(Int32 connnr, Int32 timeout, string var_type, string var_format, UInt16 var_offset, UInt16 var_bit, UInt16 var_DB_no, out UInt32 var_result, out Single var_result_float)
{
Int32 result = AGL4.AGL40_PARAMETER_ERROR;
AGL4.DATA_RW40[] rwfield = new AGL4.DATA_RW40[1];
for (int j = 0; j <= 2; j++)
{
rwfield[j] = new AGL4.DATA_RW40();
rwfield[j].BitNr = var_bit;
rwfield[j].DBNr = var_DB_no;
rwfield[j].Offset = var_offset;
rwfield[j].OpAnz = 1;
rwfield[j].Result = 0;
switch (var_type)
{
case "M":
rwfield[j].OpArea = AGL4.AREA_FLAG;
break;
case "I":
rwfield[j].OpArea = AGL4.AREA_IN;
break;
case "O":
rwfield[j].OpArea = AGL4.AREA_OUT;
break;
}
PLC__VAR_vartype = var_type;
switch (var_format)
{
case "X":
rwfield[j].OpType = AGL4.TYP_BIT;
rwfield[j].B = new Byte[rwfield[0].OpAnz];
rwfield[j].B[0] = 0;
break;
case "B":
rwfield[j].OpType = AGL4.TYP_BYTE;
rwfield[j].B = new Byte[rwfield[0].OpAnz];
rwfield[j].B[0] = 0;
break;
}
}
result = AGL4.ReadMixEx(connnr, rwfield, rwfield.Length, timeout);
Return_ReadMixEx = result;
for (int j = 0; j <= 2; j++)
{
if (result != AGL4.AGL40_SUCCESS)
{
// Error happened.
String errormsg = "";
AGL4.GetErrorMsg(result, out Error_Message_ACCON);
var_result = 0;
var_result_float = 0;
}
else
{
switch (var_format)
{
case "X":
var_result = rwfield[j].B[0];
var_result_float = 0;
break;
case "B":
var_result = rwfield[j].B[0];
var_result_float = 0;
break;
}
}
}
return result; // Here I get the Error
}
You are right in saying you are assigning a value to the variable, the problem however is that there is a case/branch where you wont be assigning a value to the variable.
For example:
If result == AGL4.AGL40_SUCCESS
you will go to the else
. If your var_format
variable is not X or B you wont match any of the switch conditions and the var_result
will never be assigned.
Looking at the code you provide. var_result
and var_result_float
will always be 0. I would therefore suggest you assign them at the start of the method.
public Int32 doReadMixEx2(Int32 connnr, Int32 timeout, string var_type, string var_format, UInt16 var_offset, UInt16 var_bit, UInt16 var_DB_no, out UInt32 var_result, out Single var_result_float)
{
var_result = 0;
var_result_float = 0;
Int32 result = AGL4.AGL40_PARAMETER_ERROR;
AGL4.DATA_RW40[] rwfield = new AGL4.DATA_RW40[1];
//Rest of your code
return result;
}