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 ?

Thanks

Olukay
HandlingNull16022019.PNG
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 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/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

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

Link to home
membership
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

ASKER

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 ..?