C# 调用在另一个函数中返回布尔值的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15677806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling a function returning boolean in another function
提问by whoadityanawandar
I have the following function that I need to call in another function. I dont know how to do it?
我有以下函数需要在另一个函数中调用。我不知道该怎么做?
private int IsValidUser()
{
int result = 0;
string strQuery = "Select Email From AUser Where Email = @Email And Password = @Password ";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand Cmd = new SqlCommand(strQuery, con);
//Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
con.Open();
result = (int)Cmd.ExecuteScalar();
if (result > 0)
{
//Session["SessionEmail"] = txtEmail.Text;
Session[General.S_USEREMAIL] = txtEmail.Text;
Response.Redirect("~/frmMyAccountMyProfile.aspx");
}
else
{
Literal1.Text = "Invalid Email/Password!";
}
}
I am trying to call it as below on button click event.
我试图在按钮单击事件上如下调用它。
protected void btnSignIn_Click(object sender, EventArgs e)
{
// Authenticate User
bool blnValidUser = false;
IsValidUser();
blnValidUser = Convert.ToBoolean(IsValidUser().result.Value);
if (blnValidUser)
{
// on Success - If remember me > Save to cookies
//SaveUserDetailsToCookie();
}
else
{
// on Failure - Show error msg
}
}
回答by Timothy Groote
Your function IsValidUser
is designed to return an int
您的函数IsValidUser
旨在返回一个 int
(for whatever reason is unknown to me, because it returns nothing. this code will never compile.)
(不管什么原因我都不知道,因为它什么都不返回。这段代码永远不会编译。)
you can fix it, by having it return a bool
like this:
你可以修复它,让它bool
像这样返回:
private bool IsValidUser()
{
int result = 0;
//since executeScalar is intended to retreive only a single value
//from a query, we select the number of results instead of the email address
//of each matching result.
string strQuery = "Select COUNT(Email) From AUser Where Email = @Email And Password = @Password ";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand Cmd = new SqlCommand(strQuery, con);
//Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
con.Open();
result = (int)Cmd.ExecuteScalar();
//returning a boolean comparator works like this :
//will return true if the result is greater than zero, but false if it is not.
return result > 0;
}
and then you can call your function like this:
然后你可以像这样调用你的函数:
blnValidUser = IsValidUser();
回答by Kareem Khan
Modify the function as below
修改函数如下
private int IsValidUser()
{
int result = 0;
string strQuery = "Select Email From AUser Where Email = @Email And Password = @Password ";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand Cmd = new SqlCommand(strQuery, con);
//Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
con.Open();
result = (int)Cmd.ExecuteScalar();
if (result > 0)
{
//Session["SessionEmail"] = txtEmail.Text;
Session[General.S_USEREMAIL] = txtEmail.Text;
Response.Redirect("~/frmMyAccountMyProfile.aspx");
}
else
{
Literal1.Text = "Invalid Email/Password!";
}
return result;
}
}
And call it as follows
并按如下方式调用它
protected void btnSignIn_Click(object sender, EventArgs e)
{
// Authenticate User
int blnValidUser = IsValidUser();
//Check if blnValidUser is greater than 0.
//IsValidUser() will return value greater than 0 if the sql is successful else will return 0
if (blnValidUser > 0)
{
// on Success - If remember me > Save to cookies
//SaveUserDetailsToCookie();
}
else
{
// on Failure - Show error msg
}
}
回答by Tim Schmelter
IsValidUser
returns an int
which clearly has no result
property. But i assume that you just want to use that value:
IsValidUser
返回一个int
显然没有result
属性的。但我假设您只想使用该值:
int valUserResult = IsValidUser();
bool blnValidUser = valUserResult == 1;
But you should consider to return a bool
in the first place because that would be more readable:
但是您应该首先考虑返回 a bool
,因为这会更具可读性:
private bool IsValidUser()
{
bool result = false;
// ...
return result;
}