convert void to string in c#

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
The code below. I would like to convert to   public string GetTextBoxValue1(Control parent)
and the return value will be  finalstr


Can you show me how to change it?


 public void GetTextBoxValue1(Control parent)
    {
        string str = string.Empty;
        string str1 = string.Empty;
        string  finalstr = string.Empty;
        foreach (Control x in parent.Controls)
        {
            if ((x.GetType() == typeof(TextBox)))
            {
                TextBox t = ((TextBox)(x));
                //Response.Write(t.ID + " " + t.Text + "<br>");
                finalstr=finalstr + "&" + t.ID + "=" + t.Text;
                str = str + ("&" + t.ID + "=" + t.Text);
            }
            if (x.HasControls())
            {
                //Response.Write("NONE");
                  GetTextBoxValue1(x);
            }
        }
        
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Have a look at this:

public string GetTextBoxValue1(Control parent)
    {
        string str = string.Empty;
        string str1 = string.Empty;
        string  finalstr = string.Empty;
        foreach (Control x in parent.Controls)
        {
            if ((x.GetType() == typeof(TextBox)))
            {
                TextBox t = ((TextBox)(x));
                //Response.Write(t.ID + " " + t.Text + "<br>");
                finalstr=finalstr + "&" + t.ID + "=" + t.Text;
                str = str + ("&" + t.ID + "=" + t.Text);
            }
            if (x.HasControls())
            {
                //Response.Write("NONE");
                  GetTextBoxValue1(x);
            }

        }

        return finalstr;       
    }

Open in new window

IT / Software Engineering Consultant
Top Expert 2016
Commented:
It feels like you want to accumulate all results into the final result returned from the first call to this recursive method, in which case I think you need an addition:

public string GetTextBoxValue1(Control parent)
    {
        string str = string.Empty;
        string str1 = string.Empty;
        string  finalstr = string.Empty;
        foreach (Control x in parent.Controls)
        {
            if ((x.GetType() == typeof(TextBox)))
            {
                TextBox t = ((TextBox)(x));
                //Response.Write(t.ID + " " + t.Text + "<br>");
                finalstr = finalstr + "&" + t.ID + "=" + t.Text;
                str = str + ("&" + t.ID + "=" + t.Text);
            }
            if (x.HasControls())
            {
                //Response.Write("NONE");
                finalstr = finalstr + GetTextBoxValue1(x);
            }

        }

        return finalstr;
    }

Open in new window


»bp
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
A string is not the appropriate return type for this functions

I would create a list of objects that have ID and Text attributes and add to it in each iteration

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial