简体   繁体   中英

Local Variable C#

how can I get a local variable?

I have this code

if (ctrl is Control)
{
    Control c = (Control)ctrl;
    foreach (object innerCtrl in c.Controls)
    {
        if (innerCtrl is System.Web.UI.WebControls.CheckBox)
        if (((CheckBox)innerCtrl).Checked == true)
        {
            string resultado = (((CheckBox)innerCtrl).Text);
        }
        else
        {
            TextBox1.Text = "não";
        }
    }
}

how can I get the variable resultado ?

This answer makes a huge assumption about what you mean by, "how can I get the variable" (you already have the variable...).

You can declare the variable at any scope you need it:

string resultado = null;
if (ctrl is Control)
{
    Control c = (Control)ctrl;
    foreach (object innerCtrl in c.Controls)
    {
        if (innerCtrl is System.Web.UI.WebControls.CheckBox)
        {
            if (((CheckBox)innerCtrl).Checked)
            {
                resultado = (((CheckBox)innerCtrl).Text);
            }
            else
            {
                TextBox1.Text = "não";
            }
        }
    }
}

if (resultado != null) /* use the variable */

Change the definition to something that will compile: It appears that you were just attempting to bold the variable name and it came off with asterisks around it - my apologies.

If you want to get at it outside the loop, then define it above with c like this:

Control c = (Control)ctrl;
string resultado = null;

and then just use it later like this:

resultado = (((CheckBox)innerCtrl).Text);

and if you want the variable to be global then define it in the class. Assume the class is something like this:

public class YourClass
{
    string resultado = null;
}

and now you don't need to define it anywhere else, just use it in the method.

Assuming that you might get multiple results. I would recommend using a List to store all the results. This way you can access the List after the function has completed and use that to access all the values.

List<String> results = new List<String>;

if (ctrl is Control)
{
    Control c = (Control)ctrl;
    foreach (object innerCtrl in c.Controls)
    {
        if (innerCtrl is System.Web.UI.WebControls.CheckBox)
            if (((CheckBox)innerCtrl).Checked == true)
            {
                string resultado = (((CheckBox)innerCtrl).Text);
                if (!String.IsNullOrEmpty(resultado))
                    results.Add(resultado);
            }
            else
            {
                TextBox1.Text = "não";
            }
    }
}

if (results.Count > 0)
{
    // We got results. Do something with our results.
    foreach (var result in results)
    {
        Console.Write(results);
    }
}

I would recommend that you move your code into it's own function. Normally I prefer to have my functions do only one thing to keep it simple, but as a start you could turn it into something like this.

public List<String> FetchTextFromCheckBoxes(Control cntrl)
{
    List<String> results = new List<String>();

    if (ctrl is Control)
    {
        Control c = (Control)ctrl;
        foreach (object innerCtrl in c.Controls)
        {
            if (innerCtrl is System.Web.UI.WebControls.CheckBox)
                if (((CheckBox)innerCtrl).Checked == true)
                {
                    string resultado = (((CheckBox)innerCtrl).Text);
                    if (!String.IsNullOrEmpty(resultado))
                        results.Add(resultado);
                }
                else
                {
                    TextBox1.Text = "não";
                }
        }
    }

    return results;
}

Then you simply use it like this in the main part of your application.

List<String> results = FetchTextFromCheckBoxes(ctrl);

if (results.Count > 0)
{
    // We got results. Do something with our results.
    foreach (var result in results)
    {
        Console.Write(results);
    }
}

Considering that local variables in the method are always declared on the top of the scope by compiler, no matter the real visibility scope in the code. So there is no any performance/memory impact in moving variable out of if statement, like this:

foreach (object innerCtrl in c.Controls)
{
     if (innerCtrl is System.Web.UI.WebControls.CheckBox) {

        string resultado = string.Empty;
        if (((CheckBox)innerCtrl).Checked)
        {
             resultado = (((CheckBox)innerCtrl).Text);
        }
        else
        {
           TextBox1.Text = "não";
        }

        //CAN READ variable here
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM