从函数返回值时出错 [英] Error while returning value from function

查看:159
本文介绍了从函数返回值时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
查看我的代码,

Hello Friends,
Go through my code,

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        clsUser objUser = new clsUser();
        Hashtable hstUser = new Hashtable();
        hstUser.Add("username", Login1.UserName);
        hstUser.Add("pwd", Login1.Password);
        objUser.setDetails(hstUser);

        if (Validate(objUser) == true)
        {
            Response.Redirect("..\\Pages\\frmRegistration.aspx");
        }
        else
            e.Authenticated = false;
    }
    private bool Validate(clsUser objUser) //Line1
    {
        UserBLL objUserBLL = new UserBLL();
        int intResult = objUserBLL.ValidateLogin(objUser);
        if(intResult > 0)
            return true;
        else if(intResult < 0)
            return false;
    }



我在Line1遇到错误,说:



I''m getting error at Line1 saying:

''_Default.Validate(clsUser)'': not all code paths return a value	<br />



谁能帮我解决问题?

在此先感谢.



Can anyone help where I''m going wrong?

Thanks in advance.

推荐答案

如果您声明一个函数以返回特定类型的值(即,除void以外的其他值),则必须确保所有可能的方法退出函数(在函数末尾隐式完成,或使用return语句显式)的返回值与声明的数据类型兼容(例如null将与字符串和Object数据类型兼容).

声明一个变量并将其用作回报.
If you declare a function to return a particular type of value (ie. something other than void), then you must ensure that all possible ways of exiting the function (implicit finish at the end of the function, or explicit with the use of a return statement) return a value compatible with the declared data type (eg. null would be compatible with both the string and Object data types).

Declare one variable and use it in return.
private bool Validate(clsUser objUser) //Line1
        {
            bool flag = false;

            UserBLL objUserBLL = new UserBLL();
            int intResult = objUserBLL.ValidateLogin(objUser);
            if (intResult > 0)
                flag = true;
            else if (intResult < 0)
                flag = false;

            return flag;
        }


在诸如
这样的validate函数的末尾写入
write
return true


private bool Validate(clsUser objUser) //Line1
    {
        UserBLL objUserBLL = new UserBLL();
        int intResult = objUserBLL.ValidateLogin(objUser);
        if(intResult > 0)
            return true;
        else if(intResult < 0)
            return false;
       return true;
    }


这篇关于从函数返回值时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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