C
C#10mo ago
Mekasu0124

✅ Index expressions are only supported with constants - Avalonia ReactiveUI

IObservable<bool> okUsername = (IObservable<bool>)this.WhenAnyValue(
x => x.Username,
x => IValidate.IsValidUsername(x.Username));

public class InputValidation
{
public bool IsValidUsername(string username)
{
if (string.IsNullOrEmpty(username))
{
return false;
}

string pattern = @"^[a-zA-Z0-9!@#$%^&*_-]{7,12}$";
return Regex.IsMatch(username, pattern);
}
IObservable<bool> okUsername = (IObservable<bool>)this.WhenAnyValue(
x => x.Username,
x => IValidate.IsValidUsername(x.Username));

public class InputValidation
{
public bool IsValidUsername(string username)
{
if (string.IsNullOrEmpty(username))
{
return false;
}

string pattern = @"^[a-zA-Z0-9!@#$%^&*_-]{7,12}$";
return Regex.IsMatch(username, pattern);
}
I keep getting a run-time error (only shows up when I attempt to launch the application) that says Index expressions are only supported with constants on the IObservable<bool> and I'm not quite understanding why and google doesn't offer much help.
14 Replies
Buddy
Buddy10mo ago
do not reuse x for one
Mekasu0124
Mekasu0124OP10mo ago
what do you mean? https://www.reactiveui.net/docs/handbook/commands/ That's how the library shows to use it
Buddy
Buddy10mo ago
I assume this is for CanExecute?
Mekasu0124
Mekasu0124OP10mo ago
correct
Buddy
Buddy10mo ago
https://www.reactiveui.net/docs/handbook/when-any#reactivecommandcanexecute-observable I have rarely ever used ReactiveUI, but I assume you cannot use an instance of an object, the method in question must be static
Mekasu0124
Mekasu0124OP10mo ago
var canExecute = this.WhenAnyValue(
x => x.Username, x => x.Password, x => x.ConfirmPassword,
(user, pass, confPass) =>
!string.IsNullOrWhiteSpace(user) &&
!string.IsNullOrWhiteSpace(pass) &&
!string.IsNullOrWhiteSpace(confPass) &&
pass == confPass);

Create = ReactiveCommand.Create(ReturnNewUser, canExecute);
var canExecute = this.WhenAnyValue(
x => x.Username, x => x.Password, x => x.ConfirmPassword,
(user, pass, confPass) =>
!string.IsNullOrWhiteSpace(user) &&
!string.IsNullOrWhiteSpace(pass) &&
!string.IsNullOrWhiteSpace(confPass) &&
pass == confPass);

Create = ReactiveCommand.Create(ReturnNewUser, canExecute);
so then it would be something like this? Which I mirrored from https://www.reactiveui.net/docs/handbook/when-any#reactivecommandcanexecute-observable
Buddy
Buddy10mo ago
Pretty much, yeah.
Mekasu0124
Mekasu0124OP10mo ago
ok bet. tyvm. you just helped me save lines of code on other projects that do multiple IObservables and then concat then all together ❤️
Buddy
Buddy10mo ago
Also good to add DistinctUntilChanged So it does not spam you
This operator stands guard, ensuring that you're not bombarded with repetitive information. Imagine if your best friend kept repeating the same story to you every time you met. It'd get old, right? The DistinctUntilChanged operator does just that; it prevents subsequent identical emissions from an observable.
Mekasu0124
Mekasu0124OP10mo ago
ok where do I put it?
Buddy
Buddy10mo ago
at the end of the WhenAnyValue method call
Buddy
Buddy10mo ago
docs here links to the example that shows you how to call it
Mekasu0124
Mekasu0124OP10mo ago
I see it. tyvm ❤️
Want results from more Discord servers?
Add your server