Solved

Out Parameter in Recursive Function

Posted on 2008-10-28
7
1,318 Views
Last Modified: 2013-12-17
Hi,

I have a function that adds elements from tree to a list recursively.
I would like to put the list initialization inside the implementation of the function (There's no good reason, just to understand how)
and pass the list argument as out parameter.

The thing is when I do it I get the error saying I must assign value, because I only initialize the list the first time if it is null the else condition doesn't have initialization.
How should I write it.

Here's the pseudo code:

public func(Tree root, out IList list)
{
.....
if (root == null)
      return;
if (list == null)
      list = new List();
func(root.child,out list);
....
}

0
Comment
Question by:breezback
[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
  • 5
7 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22827169
maybe instead use the ref key?

public func(Tree root, ref IList list)
{
.....
if (root == null)
      return;
if (list == null)
      list = new List();
func(root.child,ref list);
....
}
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 500 total points
ID: 22827171
the "out" key requires that in each function call, the "out" variable is changed.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22827180
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 23098989
I should get credit for this question, as the answer I specified is correct.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 23126833
Thanks
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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