How to compare a parsed string object within a predicate to the predicate string

Viewed 104

I have this code(heavily modified for your comfort):

string password = Console.ReadLine();
var checker = new Predicate<string>[] { (s) => s == password };
string password2 = CompareStringAgainstPredicatesAndReturn(checker);

The predicate's purpose is to verify that password2 is the same as password. Yet I am getting the following error message: CS1628: Cannot use ref, out, or in parameter 'password' inside an anonymous method, lambda expression, query expression, or local function

I don't know how to parse a variable to the predicate. So how would I go about doing this?

Gonna point out one more time that this is not the actual code. The question is only regarding the actual predicate situation.

As I was afraid, the lack of a complete code caused confusion and understandable misunderstandings. Therefore, here is the full code

string password = BInput.GetPredicateString(
new string[] { "Choose your password" },
"Password: ", //Simply a prompt
"Your password needs to be between 8 to 30 characters long, contain at least 2 numbers and at least one special character", // Error message
new Predicate<string>[] {
        (s) => s.Length > 8,
        (s) => s.Length < 30,
        (s) => s == password,
        (s) => Verificator.ContainsSpecialCharacter(s) //Not native
    }
);

BInput.GetPredicateString will confirm that the returned string fulfills all requirements within the predicate. This is why the Predicate is an array.

Hoping this brings more clarity, but it's fully possible it doesn't as I've been programming now for 14 hours and brain is like a soggy sponge.

Best regards,

3 Answers

Since you updated the question with more info, here is how I would check with a predicate array:

string? password = Console.ReadLine();
string? password2 = Console.ReadLine();

Predicate<string>[] checker = new Predicate<string>[] {
        (s) => s.Length > 8,
        (s) => s.Length < 30,
        (s) => s == password
    };

//when validated, this line can be used inside your GetPredicateString method
//password2 can be passed as parameter to GetPredicateString method.
var isPasswordValid = checker.All(predicate => predicate(password2));

Here I use All method to check all predicates are true.

So if I pass

password 123456789 and password2 123456789 => will be true
password 123456789 and password2 123456788 => will be false
password 1234567 and password2 1234567 => will be false
password 123456789012345678901234567890 and password2 123456789012345678901234567890 => will be false

One thing more to mention. In your variable String password... it contains also a conditions == password, this will fail because the variable name and variable input can not have the same name.

So you need to change String password =.. to String passwordPredicated =.. or something like that.

Original Answer

I am not sure why you are using array in predicted, but if you need to check one input against the other input, by using a predicate, you can do something like this:

string? password = Console.ReadLine();
string? password2 = Console.ReadLine();

Predicate<string> checker = new Predicate<string>(s => s == password);

bool isEqual = checker(password2);

Make 2 equal inputs and you get True else it will be false.

The class Predicate is a generic one. You can create a class that encapsulates both passwords and use it with Predicate class.

class BothPasswords {
   public string Password1 { get; set; }
   public string Password2 { get; set; }
}

So, in the main method you will have, for example:

string password1 = "test";
string password2 = "test";

Predicate<BothPasswords> isUpper = objParam => objParam.Password1.Equals(objParam.Password2.ToUpper());
bool result = isUpper(new BothPasswords { Password1 = password1, Password2 = password2 });

I doubt you can use ref, out etc. Inside a lambda body.

Use a temp variable to store ref or Use a simple extension methods and chain them for this case.

public static class PasswordValidator ...
  public static bool HasValidLength(this string password)
   { 
      [condition to check] 
   }
Related