Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-16
5
Medium Priority
?
289 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:khyberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 18

Assisted Solution

by:Ravi Singh
Ravi Singh earned 500 total points
ID: 16922649
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
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16922694
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 16922704
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 16922725
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
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16922757
aha, I wasnt aware of that... your right Alex - thanks for correction
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question