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
246 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
  • 3
  • 2
5 Comments
 
LVL 18

Assisted Solution

by:Ravi Singh
Ravi Singh earned 125 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 125 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now