Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass string if the parameter value is null

Tags:

c#

I need to pass multiple parameters in a function. My requirement is the parameter value should not be NULL. If the parameter is NULL pass "TBD" instead.

e.g.

getBookInfo (string bookId, string bookName, string bookAuthor) 
//if any of the parameters is NULL, pass "TBD" string in parameter

How can I do this? Can I do this using ternary operator, and if so, how?

like image 309
user4221591 Avatar asked May 25 '15 04:05

user4221591


People also ask

Can we pass NULL value in string?

So, we can define a Predicate that will check for the null value of a String and pass this Predicate to the filter() method. Consequently, the filter will filter out those null values from the original Stream.

How do you pass a parameter as null?

You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.

Can a parameter be null?

When the parameter has no value, SQL interprets it as null in your code. Null means no value. You can fix this problem by adding a code to fix the null case. There are 3 ways to resolve this problem: you can either coalesce the null value or add a logic to execute another operation.

How do you pass a null value as a parameter in Python?

There's no null in Python. Instead, there's None. As stated already, the most accurate way to test that something has been given None as a value is to use the is identity operator, which tests that two variables refer to the same object. In Python, to represent an absence of the value, you can use a None value (types.


1 Answers

Try doing it this way when calling your method:

getBookInfo (bookId ?? "TBD", bookName ?? "TBD", bookAuthor ?? "TBD");

The ternary operator ?: is a waste when you can use the null coalescing operator ??.

like image 155
Enigmativity Avatar answered Oct 01 '22 13:10

Enigmativity