0
public class loginbal
{
  public static bool   match = false ;

  public bool check(string username, string password)
  {
        logindal LGD = new logindal();
        DataSet ds1= LGD.logincheck(username, password);

        int noofrows = ds1.Tables["login"].Rows.Count;

        for (int i = 0; i < noofrows; i++)
        {
            if ((ds1.Tables["login"].Rows[i]["username_l"].ToString() == username) && (ds1.Tables["login"].Rows[i]["password_l"].ToString() == password))
            {
                match = true;
            }
        }

    return match;
}

I want to return match but its not affected with for loop set statement what i can do to change match according to for loop value and return to method?

2
  • 4
    and why do you need a variable in the first place? just return true when you found a match. Also you are never resetting match to false so the method will always return true after you have found a match once. Commented Sep 16, 2011 at 21:33
  • 1
    Get rid of your match variable. Replace match = true with return true, and return match with return false. Presto. Commented Sep 16, 2011 at 21:35

1 Answer 1

2

As @BrokenGlass and @NullUserException have pointed out, there is no need for a variable match, much less a static one. Just return true if the loop finds a match. If it doesn't, return false.

public bool check(string username, string password)
{
    logindal LGD = new logindal();
    DataSet ds1= LGD.logincheck(username, password);

    int noofrows = ds1.Tables["login"].Rows.Count;

    for (int i = 0; i < noofrows; i++)
    {
        if ((ds1.Tables["login"].Rows[i]["username_l"].ToString() == username)
            && (ds1.Tables["login"].Rows[i]["password_l"].ToString() == password))
        {
            return true;
        }
    }

    return false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

There's no need for .Equals as the .net compiler will do this anyway. Also I rather think returning instead a loop is bad practice as you can't write a flow diagram for the function.
there is a little difference between Equals and ==. One difference is if the string you call Equals on is null, a null exception will be thrown. Not so for ==.
@Ash - Removed the .equals() distinction. My mind was stuck in Java mode, thanks. As for returning in the loop, that can be the OP's call after reading your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.