Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am confused by an error in the following code:
public static class PublicFunct
{
        public static bool CheckTxtIsNull(Control ctrl)
        {
            bool x;
            
            if (ctrl.GetType() == typeof(TextBox))
            {
                if (ctrl.Text == null || ctrl.Text == "")
                    x = false;
                else
                    x = true;                
            }
                    
            foreach (Control ctrlChild in ctrl.Controls)
            {
                CheckTxtIsNull(ctrlChild);
            }
            
            return x;
        }

Error CS0165 Use of unassigned local variable 'x'

What I have tried:

I can't resolve error. Help me please. Thank!
Posted
Updated 20-Jan-18 7:06am
Comments
Dotnet_Dotnet 20-Jan-18 4:42am    
sir simple define bool x = false;

public static bool CheckTxtIsNull(Control ctrl)
{
bool x = false;

if (ctrl.GetType() == typeof(TextBox))
{
if (ctrl.Text == null || ctrl.Text == "")
x = false;
else
x = true;
}

foreach (Control ctrlChild in ctrl.Controls)
{
CheckTxtIsNull(ctrlChild);
}

return x;
}

x gets assigned only if ctrl.GetType() == typeof(TextBox) is true (because all assignment operations happen inside that if block). If that is false, x will stay unassigned, and that's why the error happens. Make sure to assign something to x also if that statement is false.

(And, unrelated to the error, and perhaps you know, but worth pointing out anyway: you recursively run CheckTxtIsNull in the foreach but you don't do anything with that return value, so that function call is entirely useless at the moment).
 
Share this answer
 
v2
Comments
Member 13451441 20-Jan-18 10:29am    
Thank you for reply.
I fixed same solution 2 but it's impossible.
Thomas Daniels 20-Jan-18 10:31am    
Uhh.. what exactly is impossible?
Simply Define your Boolean variable as

bool x = false instead of bool x;

public static class PublicFunct
        {
            public static bool CheckTxtIsNull(Control ctrl)
            {
                bool x = false;

                if (ctrl.GetType() == typeof(TextBox))
                {
                    if (ctrl.Text == null || ctrl.Text == "")
                        x = false;
                    else
                        x = true;
                }

                foreach (Control ctrlChild in ctrl.Controls)
                {
                    CheckTxtIsNull(ctrlChild);
                }

                return x;
            }
        }
 
Share this answer
 
v2
I tried and reached the goal
Check all textbox on form, if textbox null/empty, to be cursor to location null.
Messagebox and break;
AT FORM
namespace LabWinForm
{
    public partial class frmTxtNull : Form
    {
        public frmTxtNull()
        {
            InitializeComponent();
        }

        
        private void btnCheckNull_Click(object sender, EventArgs e)
        {
			///1 - Browse all control
            foreach (Control item in this.Controls)
            {
				///2 - If control is textbox is call static funtion (CheckTxtNull) at class PublicFunct
                if (item.GetType() == typeof(TextBox))
                {
					///3 - If result = True then break;
                    if (iBookFunct.CheckTxtNull(item))
                        return;
                }
            }
			///4 - Execute when all textbox not null/empty
            MessageBox.Show("Hello world");
        }

    }	
}

AT STATIC CLASS
namespace LabWinForm
	public static class PublicFunct
    {
        /// <summary>
        /// Check textbox control is null or empty
        /// </summary>
        /// <param name="ctrl">control textbox</param>
        /// <returns></returns>
        public static bool CheckTxtNull(Control ctrl)
        {
            bool result;
            if (string.IsNullOrEmpty(ctrl.Text))
            {
                MessageBox.Show("Data fields should not be blank.");
                result = true;
                ctrl.Focus();
            }

            else
            {
                result = false;
            }
            return result;
        }


    }


Thank everyone!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900