简体   繁体   中英

Pass string if the parameter value is null

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.

eg

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?

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

Just put an if condition inside this function and check for null values of these three variables and if found null , you can assign the default values immediately after the if check and then continue further execution.

Something like this :

getBookInfo (string bookId, string bookName, string bookAuthor)
{
   bookId = (bookId == "" ) ? bookId : "TBD";
   // other variables same way.
}

Hope this clears it.

Using ternary it would be like:

getBookInfo (bookId == null ? "TBD" : bookId, bookName == null ? "TBD" : bookName, bookAuthor == null ? "TBD" : bookAuthor)

but I think that is not very clear to read...

private void GetBookInfo(string bookId, string bookName, string bookAuthor)
        {
            // if any of the parameter is NULL, set "TBD" as the value
            bookId = bookId == null ? "TBD" : bookId;
            bookName = bookName == null ? "TBD" : bookName;
            bookAuthor = bookAuthor == null ? "TBD" : bookAuthor;

            // rest of the code goes here
        }

Before passing parameter check whether parameter is null or not. For example

string bookid;
string bookName;
string bookAuthor;
if(bookid==null)
bookid="TBD";
if(bookName==null)
bookName="TBD";
if(bookAuthor==null)
bookAuthor="TBD";
getBookInfo (string bookId, string bookName, string bookAuthor)

This will set value "TBD" if value of any field would be null. Hope it resolved your issue. Thanks

You could also provide an overload of the method which would allow the developer to provide a limited number of parameters. The overload would call the method with additional parameters passing default values for parameters it doesn't already have.

public void GetBookInfo (string bookID)
{
    // Call overload passing bookID and empty string as book name
    return GetBookInfo(bookId, string.Empty);
}

public void GetBookInfo (string bookID, string bookName)
{
    // Call overload passing bookID, bookName and empty string as author
    return GetBookInfo(bookId, bookName, string.Empty);
}

public void GetBookInfo (string bookID, string bookName, string author)
{
    // All 3 parameters are populated here, get the book info...
}

With the newer versions of C# you can do this (add default values to arguments):

public void getBookInfo (string bookId = "TBD", string bookName = "TBD", string bookAuthor = "TBD") 

This also makes the arguments optional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM