如何从静态类中的staic函数返回结果? [英] How to get result return from a staic funtions in static class?

查看:83
本文介绍了如何从静态类中的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'



什么我试过了:



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


Error CS0165 Use of unassigned local variable 'x'

What I have tried:

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

推荐答案

x 只有在 ctrl.GetType()==时才会被分配typeof(TextBox)为true(因为所有赋值操作都发生在 if 块内)。如果这是错误的, x 将保持未分配状态,这就是错误发生的原因。如果该语句为假,请确保为 x 指定一些内容。



(并且,与错误无关) ,也许你知道,但值得指出:你在 foreach 中递归运行CheckTxtIsNull,但是你没有对该返回值做任何事情,所以函数调用完全是目前没用。)
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).


简单地将你的布尔变量定义为



bool x = false而不是bool x;



公共静态类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(在ctrl.Controls中控制ctrlChild)

{

CheckTxtIsNull(ctrlChild);

}



返回x;

}

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


我尝试并达到了目标

检查表单上的所有文本框,如果textbox为null / empty,则将光标移动到位置null。

Messagebox和break;

AT FORM

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!


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆