Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

How can i get my application to accept null value as valid

I have a C#  application with a variable  selectedPeriod.mnthyear

I need to have the variable accept both regular values and Null

The test  script (attached)  is ok when there is a value.

When the value of the variable  is null  the application  reports an error as attached

What is wrong with my test syntax for   selectedPeriod.mnthyear  when it is null ?


Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

I would use a try/catch block to trap these errors.
You should use Nullable Types
double? pi = 3.14;
char? letter = 'a';

int m2 = 10;
int? m = m2;

bool? flag = null;

// Array of nullable type:
int?[] arr = new int?[10];

Open in new window

Don't check for a property of selectedPeriod but for the object itself, thus:

If (selectedPeriod == null)
// <snip>

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Olukayode Oluwole


A big thank you to all the experts.

I believe any of the suggestions might have worked.

The solution from Chris Stanyon requiring only a  ?   appears easiest to implement

Once again thanks  for the help
But you don't use the property mnthyear in this part of the code, so easiest ..?