Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pass a null as a one param argument

I have a method that accepts params as a parameter to check if a property has invalid value or not. If the property has an invalid value, it throws an exception

private void ShouldNotEqual<T>(Expression<Func<Ticket, T>> outExpr, params object[] notValidPropertyValue)

The usage is:

ShouldNotEqual(student => student.Name, "jack", string.Empty);
ShouldNotEqual(student => student.Address, string.Empty, null);

The problem with this implementation is when I want to check a property against null, the parameter notValidPropertyValue is set as null, not an array with one element whose value is equal to null.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// I need to use it in this way
ShouldNotEqual(student => student.Phone, null); // Throws NullReferenceException for notValidPropertyValue

How can I fix it?

>Solution :

Are you sure you want null in the array and not just an empty array?

If you want null, use

var array = new object[] { null };

to create an array containing one value, null.

Then call ShouldNotEqual with the array, e.g.

ShouldNotEqual(student => student.Phone, array);

Or just inline it:

ShouldNotEqual(student => student.Phone, new object[] { null });

If you want an empty array, then just use

ShouldNotEqual(student => student.Phone, new object[0]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading