?
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
?
287 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is about my first experience with programming Arduino.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

770 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