S5498658 S5498658 - 3 months ago 6
C# Question

Can't get the correct Bool result in C#

I have a form where I would like to check for validation before processing the form. My form has 2 sections so I want to make sure that at least one item from each section is selected when they press submit button, and if they did then go to Dashboard.aspx. Even if I put all the required info when it checks for result1 and result2, I get false. Result1 and Result2 won't get the correct value. Even if the values are True again it passes false.

Here is my code:

protected void btnSumbit_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;

bool result1 = false;
bool result2 = false;
CheckWireFromValidation(result1);
CheckWireToValidation(result2);
if (result1 == true && result2 == true)
{
Response.Redirect("~/DashBoard.aspx");
}
}

public bool CheckWireFromValidation (bool result1)
{
if (drpFromCoporate.SelectedIndex != 0 || drpFromCapital.SelectedIndex != 0 || drpFromProperty.SelectedIndex != 0)
{
result1 = true;
}
else
{
result1 = false;
ShowAlertMessage("You need to choose at least one filed from Wire From drop downs!!");
}
return result1;
}


public bool CheckWireToValidation(bool result2)
{
if (drpToCapital.SelectedIndex != 0 || drpToCoporate.SelectedIndex != 0 || drpToProperty.SelectedIndex != 0 || drpToTemplate.SelectedIndex != 0 || txtCorpAmt.Text != "" || txtCapAmt.Text != "" || txtPropAmt.Text != "" || txtTempelateAmt.Text != "")
{
result2 = true;
}
else
{
ShowAlertMessage("You need to choose at least one filed from Wire To drop downs!!");
}
return result2;
}

Answer

You're not using the results of CheckWireToValidation. You're using the false value you allocate initially.

Try this

    bool result1 = false;
    bool result2 = false;

    if (CheckWireFromValidation(result1) && CheckWireToValidation(result2))
    {
        Response.Redirect("~/DashBoard.aspx");
    }

Edit

The behavior you're expecting is that of the out parameter modifier. But please don't write code that way ...

I edited your code to get rid of .. em .. cruft. This should be more readable.

protected void btnSumbit_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
        return;

    if (CheckWireFromValidation() && CheckWireToValidation())
    {
        Response.Redirect("~/DashBoard.aspx");
    }
}

public bool CheckWireFromValidation ()
{
    if (drpFromCoporate.SelectedIndex != 0 || drpFromCapital.SelectedIndex != 0 || drpFromProperty.SelectedIndex != 0)
    {
        return true;
    }
    else
    {          
        ShowAlertMessage("You need to choose at least one filed from Wire From drop downs!!");
        return false;
    }         
}

public bool CheckWireToValidation ()
{
    if (drpToCapital.SelectedIndex != 0 || drpToCoporate.SelectedIndex != 0 || drpToProperty.SelectedIndex != 0 || drpToTemplate.SelectedIndex != 0 || txtCorpAmt.Text != "" || txtCapAmt.Text != "" || txtPropAmt.Text != "" || txtTempelateAmt.Text != "")
    {
        return true;
    }
    else
    {
        ShowAlertMessage("You need to choose at least one filed from Wire To drop downs!!");
        return false;
    }     
}