I have a method that returns an object and also has an out parameter. The method calls another method that takes in the same out paramter as another out paramter. This gives a build error on the return statement:
The out parameter ‘param1’ must be assigned to before control leaves
the current method
The code looks like:
public TypeA Method1(TypeA param1, out bool param2)
{
/... some logic here .../
SubMethod(out param2);
/... some logic here .../
return param1;
}
param2 is manipulated in SubMethod(), not in Method1(). Is there something else I need to do?
In this case, I will assign a ‘default’ value. Regardless of bool, int, myFoo, etc. – set a default value.
But you need to identify why the exception refers to ‘param1’ when param1 is clearly not at fault in this example ( for clarification : assuming
TypeB : TypeAand is properly constrained).I believe that passing
param2as anoutparameter inSubMethod(...)removes the obligation to assignparam2. However, you have not assigned anything toparam1. Is there more going on here that has not been explained?