C# Validate two properties via lambda expression

I have the below code that works.  I want it to validate two properties.
ViewChartCommand = new DelegateCommand(OnViewChartCommandExecute,(() => !(String.IsNullOrEmpty(_duration) ))).ObservesProperty(() => (SelectedDuration));

Open in new window

I tried to implement this but it looks like it is not working.
ViewChartCommand = new DelegateCommand(OnViewChartCommandExecute,(() => !(String.IsNullOrEmpty(_duration) || String.IsNullOrEmpty(_diagnostic)))).ObservesProperty(() => (SelectedDuration)).ObservesProperty(() => SelectedDiagnostic);

Open in new window

How do I check two properties?
LVL 2
CipherISAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
First of all: rewrite it. It's Obviously too complex. Remove unnecessary parentheses. And split the calls.

e.g. the first should be

var delegateCommand = new DelegateCommand(
	OnViewChartCommandExecute,
	() => !String.IsNullOrEmpty(_duration)
	);

ViewChartCommand = delegateCommand.ObservesProperty(
	() => SelectedDuration
	);

Open in new window


What properties are you trying to validate? When should that happen?

What lambda? There are two?
0
CipherISAuthor Commented:
_duration holds the value from the Duration Property.  _diagnostic is the value for the Diagnostic property.
0
ste5anSenior DeveloperCommented:
What is SelectedDuration? Without context (code, classes involved) it's hard to guess..

Assuming that my rewrite is correct, then you may try:

var delegateCommand = new DelegateCommand(
	OnViewChartCommandExecute,
	() => !String.IsNullOrEmpty(_duration) && !String.IsNullOrEmpty(_diagnostic)
	);

ViewChartCommand = delegateCommand.ObservesProperty(
	() => SelectedDuration || SelectedDiagnostic
	);

Open in new window


imho the first test must be using a Boolean AND to avoid errors. Maybe an OR is also sufficient. But the key is that I assume SelectedSomething is a Boolean, thus here we need an OR instead of a concatenation of lambdas, cause only one SelectedSomething being true at a time seems logical to me.
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

CipherISAuthor Commented:
Thanks, I will test it in a bit.  both _duration and _diagnostic cannot be empty.

SelectedDuration and SelectedDiagnostic both have get; set; and are of string value.  Below are the properties.

private string _duration;
public string SelectedDuration
{
	get { return _duration; }
	set
	{
		SetProperty(ref _duration, value);
	}
}

private string _diagnostic;
public string SelectedDiagnostic
{
	get { return _diagnostic; }
	set
	{
		SetProperty(ref _diagnostic, value);
	}
}

Open in new window

0
ste5anSenior DeveloperCommented:
both _duration and _diagnostic cannot be empty.
Why do you then check their content?

SelectedDuration and SelectedDiagnostic both have get; set; and are of string value.

hmm, my spider senses are tingling.. you should either start explaining the context or reconsider your approach. Cause in this case this seems what you need

var delegateCommand = new DelegateCommand(
	OnViewChartCommandExecute,
	() => true
	);

ViewChartCommand = delegateCommand.ObservesProperty(
	() => SelectedDuration + " - " &  SelectedDiagnostic
	);

Open in new window


but I doubt it makes much sense.
0
CipherISAuthor Commented:
This is a Xamarin App.  The values are coming from a picker.  Need to ensure that both of the controls has a value before user can move on to next screen.
0
CipherISAuthor Commented:
I am getting an error.  I've attached images to show what the error is.
DelegateCommandError_001.png
DelegateCommandError_002.png
0
CipherISAuthor Commented:
Here is the fix.  Thx for your code which helped.
ViewChartCommand = new DelegateCommand(OnViewChartCommandExecute, () => !String.IsNullOrEmpty(_duration) && !String.IsNullOrEmpty(_diagnostic));

ViewChartCommand.ObservesProperty(() => SelectedDuration);
ViewChartCommand.ObservesProperty(() => SelectedDiagnostic);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CipherISAuthor Commented:
ste5an provided sample code which helped me come up with the solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.