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?
 
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
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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:
ste5an provided sample code which helped me come up with the solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.