문제

I want to refactor a method but I'm not too sure how to but I know you can do it.

My current method:

public bool AdminShutdown(int accountID, int accountPin)
        {
            var status = false;
            if (accountID == AdminLogin && accountPin == AdminPin)
            {
                status = true;
            }

            return status;
        }

I think it should be something like var status = (accountID == AdminLogin) && (accountPin == AdminPin) but that doesn't work ( Operator '&&' cannot be applied to operands of type 'bool' and 'int').

Suggestions?

P.S. Would this code work?

var tempReturnPerson = AccountHoldersList.Single((x => x.AccountNumber == accountId));

instead of:

public AccountHolders ReturnAccountInfo(int accountId)
        {
            //use linq to do this later
            var returnPerson = new AccountHolders();
            foreach (var person in AccountHoldersList)
            {
                if (accountId == person.AccountNumber)
                {
                    returnPerson = person;
                    break;
                }
            }

            return returnPerson;
        }
도움이 되었습니까?

해결책

if AdminLogin and AdminPin are int, than

public bool AdminShutdown(int accountID, int accountPin)
{
    return (accountID == AdminLogin && accountPin == AdminPin);
}

The error message you get make me think you may have used an = instead of an ==

EDIT

For your second question

var tempReturnPerson = AccountHoldersList.Single((x => x.AccountNumber == accountId));

should rather be

var tempReturnPerson = AccountHoldersList.FirstOrDefault(x => x.AccountNumber == accountId) ?? new AccountHolders();

If you want the same result as your method.

다른 팁

"(accountID == AdminLogin && accountPin == AdminPin)" can simply be evaluated to a bool

public bool AdminShutdown(int accountID, int accountPin)
{
    return (accountID == AdminLogin && accountPin == AdminPin)
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top