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
259 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
topping1 challenge 7 71
firstChar challenge 13 106
firstswap challenge 20 65
A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

937 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

1 Experts available now in Live!

Get 1:1 Help Now