1

Given the following simplified code block in ASP.NET

<% foreach( item in Model) { %>

    <%=item.OriginalText.OriginalText1 %>
<%} %>

OriginalText is a class which may be null. I am looking for a simple (clean) way to check for this null and return an empty string ("") instead.

6 Answers 6

9
<% foreach( item in Model) { %>

    <%= (item.OriginalText == null) ? string.Empty: item.OriginalText.OriginalText1 %>
<%} %>
Sign up to request clarification or add additional context in comments.

5 Comments

why using string.empty is better than ""?
string.Empty doesn't create an object while "" creates a string object. So using string.Empty is better than using “” because every time you use "", it creates a new string while string.Empty just reference a string in memory that is created by default.
Brettski, I didn't know there was that distiction. Thanks for the information.
I am thinking the the Null coalescing operator solution (currently below) is a better overall solution. PK :-)
I marked this as the correct answer, because basically it is; though it is not good practice. I strongly suggest that anyone think twice about actually doing this check at the page (view) level. This type of null object needs to be checked where the object is being created or put together (e.g. the model) and handled there. This way you are not checking all over the place for the null. Null is being checked in one place, being corrected and it is then ok throughout the rest of the application.
2

use this one:

<%=item.OriginalText == null ? "" : item.OriginalText.OriginalText1 %>

1 Comment

Yeah, was my first thought, I was just wondering if there was something else available. Thank you for the response.
1

If you are in control of the code, you could also consider implementing the null object pattern, thus avoiding the need to check for null.

The benefit to this approach is that you no longer have to check every time you want to use the object (Don't Repeat Yourself), but it does depend upon how you are creating your objects, and how much control you have.

Something like:

public class NullOriginalText : OriginalText
{
    public string OriginalText1 { get; private set; }

    public NullOriginalText()
    {
        OriginalText1 = string.Empty;
    }
}

public class ModelItem
{
    public OriginalText { get; private set; }

    public ModelItem()
    {
         OriginalText = new NullOriginalText();
    }

    public ModelItem(OriginalText originalText)
    {
         if (originalText == null)
         {
             OriginalText = new NullOriginalText();
         }
         else
         {
             OriginalText = originalText;
         }
    }
}

1 Comment

Si, this is pretty close to the last approach I used. I check for the null object in the Model and create a new blank object if it is null. I guess the more I looked at this it didn't make sense to be checking this over and over again at the view level. It just doesn't belong there.
0

The cleanest way I've found to do this is with an extension method.

Example:

public static class OriginalTextExtensions
{
    public static string OriginalText1SafeString(this OriginalText original)
    {
        return original == null ? string.Empty : original.OriginalText1;
    } 
}

and then you'd use it like this:

<% foreach( item in Model) { %>

    <%= item.OriginalText.OriginalText1SafeString() %>

<%} %>

Comments

0
<% foreach( item in Model) { %>

    <%= item.OriginalText == null ? string.Empty: 
                                    item.OriginalText.OriginalText1 %>
<%} %>

1 Comment

Author use in question 'item.OriginalText.OriginalText1'. Preet Sangha use in answer 'item.OriginalText1'. I use in my answer author's variant.
0

Depending on the situation the Null coalescing operator may help as well.

<% foreach( item in Model) { %>
    <%= item.OriginalText.OriginalText1 ?? String.Empty %>
<%} %> 

3 Comments

At first I thought this is what I was looking for. It looked right and impossible to search for. Though, the real issue is that it will not work. OriginalText is the null object, and trying to check for null on OriginalText.OriginalText1 throws a NullReferenceException. This would work if OriginalText is not null and the OriginalText1 was the null object. To properly check this, which doesn't make sense is: <%= item.OriginalText ?? String.Empty %>
That's why I said it depends on the situation. While it may not work in your situation, the information is good.
You could also do something like this: <%= (item.OriginalText ?? new OriginalText()).OriginalText1 %> Something like that, don't hold me to that syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.