Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

convert void to string in c#

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

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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