• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

Iterative function in C# from VB.Net

When I am trying to convert the following code in C# I used the attached C# code.            

Public Sub suCheckChild(ByRef InTn As TreeNode, ByRef InChecked As Boolean)
            Try
                If InTn Is Nothing Then Exit Sub
                InTn.Checked() = InChecked
                If InTn.Nodes.Count > 0 Then
                    For Each tn As TreeNode In InTn.Nodes
                        suCheckChild(tn, InChecked)
                    Next
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "suCheckChild01", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub

but ,

In the line

if (fCheckChild(ref tn, ref InCheckStatus, out OutError) == false) throw new Exception(OutError);

"ref tn" portion is throwing error as

"Error      1      Cannot pass 'tn' as a ref or out argument because it is a 'foreach iteration variable'      Y:\EPM\Project Architect\Net\Project Architect\Project Architect\frmPA.cs      4862      41      Project Architect
"

Is any solution?
private bool fCheckChild(ref TreeNode InTreeNode, ref bool InCheckStatus, out string OutError)
        {
            OutError = "";
            try
            {
                if (InTreeNode == null) return true;
                InTreeNode.Checked = InCheckStatus;
                if (InTreeNode.Nodes.Count <= 0) return true;
                foreach (TreeNode tn in InTreeNode.Nodes)
                    if (fCheckChild(ref tn, ref InCheckStatus, out OutError) == false) throw new Exception(OutError);
                return true;
            }
            catch (Exception ex)
            {
                OutError = ex.Message;
                return false;
            }
        }

Open in new window

0
Amitava_Mukherjee
Asked:
Amitava_Mukherjee
  • 2
1 Solution
 
Jeff CertainCommented:
There's no reason either of those variables need to be ByRef. Try changing them to ByVal and see if it works.
0
 
Jeff CertainCommented:
       private bool fCheckChild(TreeNode InTreeNode, bool InCheckStatus, out string OutError)
        {
            OutError = "";
            try
            {
                if (InTreeNode == null) return true;
                InTreeNode.Checked = InCheckStatus;
                if (InTreeNode.Nodes.Count <= 0) return true;
                foreach (TreeNode tn in InTreeNode.Nodes)
                    if (fCheckChild(tn, InCheckStatus, out OutError) == false) throw new Exception(OutError);
                return true;
            }
            catch (Exception ex)
            {
                OutError = ex.Message;
                return false;
            }
        }
0
 
Amitava_MukherjeeAuthor Commented:
Nice solution, thanx
0
 
Mohamed AbowardaSoftware EngineerCommented:
Basically, you are using passing the parameter ByRef in the method suCheckChild() which is pointless, since foreach change the variable multiple times, there is no reason to pass the parameter ByRef.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now