本文介绍了如何从静态类中的staic函数返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码中的错误感到困惑:

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;
        }



错误CS0165使用未分配的本地变量'x'



什么我试过了:



我无法解决错误。请帮帮我。谢谢!


ErrorCS0165Use of unassigned local variable 'x'

What I have tried:

I can't resolve error. Help me please. Thank!

推荐答案



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!


这篇关于如何从静态类中的staic函数返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:05