[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • 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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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