943,552 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4666
  • C# RSS
Feb 19th, 2009
0

Recursion In C#

Expand Post »
The RemoveMin class should remove the first occurrence of the smallest element from the input list. Despite what the inputs are I keep getting the same NullReference error from having 'rem = null'. I don't know if I'm using recursion right or not. I have a feeling the starting reference for the ConsList may not be right. This was a homework assignment but it was due a couple days ago. I just want to understand for future references what I need to do. Anything pointers or examples would be appreciated. Here is the line of code giving me problems.
C# Syntax (Toggle Plain Text)
  1. public static ConsList<int> RemoveMin(ConsList<int> list, out int min)
  2. {
  3. ConsList<int> r;
  4. min = list.Head;
  5. if (list.Tail.Head > min)
  6. {
  7. r = new ConsList<int>(list.Head, RemoveMin(list.Tail, out min));
  8. return r;
  9. }
  10. else
  11. {
  12. r = new ConsList<int>(min, RemoveMin(list.Tail, out min));
  13. return r;
  14. }
  15. }

Here is the class that implements RemoveMin. 'rem' keeps causing a null reference. This class shouldn't need any editing.

C# Syntax (Toggle Plain Text)
  1. private ConsList<int> SelectionSort(ConsList<int> list)
  2. {
  3. if (list == null)
  4. {
  5. return list;
  6. }
  7. else
  8. {
  9. int min;
  10. ConsList<int> rem = RemoveMin(list, out min);
  11. return new ConsList<int>(min, SelectionSort(rem));
  12. }
  13. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
staplesauce is offline Offline
1 posts
since Feb 2009
Feb 19th, 2009
0

Re: Recursion In C#

Care to post the full code unit so I can load it up in a debugger and test without putting it back together?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Feb 19th, 2009
0

Re: Recursion In C#

The problem might be in the creation of the object.
Passing a recursive parameter can be a trouble, becouse every single call to the function will create a different object an you're using a type out value wich has no value.
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: vst plugins
Next Thread in C# Forum Timeline: dynamically create datagrid with prepopulated text boxes from datatable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC