Avatar of Olukayode Oluwole
Olukayode Oluwole
Flag 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 undefined
Last Comment
Gustav Brock

8/22/2022 - Mon
David Johnson, CD

I would use a try/catch block to trap these errors.
Chinmay Patel

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

Chinmay Patel

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
Gustav Brock

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

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

Open in new window

Chris Stanyon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Gustav Brock

But you don't use the property mnthyear in this part of the code, so easiest ..?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.