Home > Blockchain >  C# How to compare a parsed string object within a predicate to the predicate string
C# How to compare a parsed string object within a predicate to the predicate string

Time:05-22

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,

CodePudding user response:

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 predicate, you can do some thing 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 input and you get True else it will be false.

CodePudding user response:

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 });

CodePudding user response:

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