I wan to pass an argument to a C# function by reference that is a User Defined Class

RiskParams risk = null;
IsPartialFill(.... risk);

            
private bool IsPartialFill(Transaction3Arb trans, bool addtopendingQ, RiskParams risk)
{
      risk = new RiskParam();
}

Please advice on how to accomplish this.


Thx
khybermanAsked:
Who is Participating?
 
AlexFMConnect With a Mentor Commented:
Small addition: if function IsPartialFill is supposed to create always RiskParams instance, you can use "out" instead of "ref". In this case compiler doesn't require to initialize risk variable before IsPartialFill call.
0
 
Ravi SinghConnect With a Mentor Senior Software EngineerCommented:
Hi, use the "ref" keyword in the formal and actual parameters:

RiskParams risk = null;
IsPartialFill(..., ref risk);

private bool IsPartialFill(Transaction3Arb trans, bool addtopendingQ, ref RiskParams risk)
{
      risk = new RiskParam();
}
0
 
Ravi SinghSenior Software EngineerCommented:
Reference types that are sent in parameters in C# are sent by reference, by default, so in your case I don't think its necessary to use the ref keyword. Its usually used for value types that need to be sent by reference.
0
 
AlexFMCommented:
Zephyr__ , your first post was correct, and your correction is wrong :(
To create instance inside of function, parameter must be passed by reference. Otherwise created object is not visible to caller.
0
 
Ravi SinghSenior Software EngineerCommented:
aha, I wasnt aware of that... your right Alex - thanks for correction
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.