使用嵌套属性忽略“对象引用未设置为对象实例”错误的最佳方法是什么?

时间:2022-01-28 23:01:16

I'm running across lots of cases where I want to display something along the lines of

我遇到了许多我希望展示某些内容的案例

@ev.Event.Instructor.Name

But the instructor property is allowed to be null. In those cases the "Object reference not set to an instance of an object." error is thrown but I'd like to ignore that and have nothing returned..

但是讲师属性可以为null。在这些情况下,“对象引用未设置为对象的实例”。错误被抛出,但我想忽略它并且没有任何返回..

Is there a better way to handle this than to create lots of ternary expressions all over the place to check for null?

是否有更好的方法来处理这个问题,而不是在整个地方创建大量的三元表达式来检查null?

The equivalent php expression would be

等效的php表达式将是

@$ev.Event.Instructor.Name

I've been translating some webforms views to MVC and the equivalent Eval statement would ignore null reference errors.

我一直在将一些webforms视图转换为MVC,等效的Eval语句会忽略空引用错误。

To clarify: The @ev property is coming from a linq query and there are also cases where I have

澄清一下:@ev属性来自linq查询,也有我的情况

@ev.mainContact.FirstName @ev.mainContact.LastName
@ev.mainContact.Company.Name

Where not only the mainContact can be null but the mainContact's company can also be null. It seems like an equally bad solution to select every single attribute with a ternary checking for null.

不仅mainContact可以为null,而mainContact的公司也可以为null。使用三元检查null来选择每个属性似乎是一个同样糟糕的解决方案。

5 个解决方案

#1


4  

One way is to add another property to Event:

一种方法是向Event添加另一个属性:

public string InstructorName
{
    get { return Instructor == null ? string.Empty : Instructor.Name; }
}

#2


3  

There is no such thing as "ignoring exception". You should not cause exceptions or should handle them.

没有“忽略例外”这样的事情。您不应该导致异常或应该处理它们。

public class Event {
     public Event() {
          this.Instructor = new Instructor();
     }
}

or

@(ev.Event.Instructor == null ? String.Empty : ev.Event.Instructor.Name)

#3


0  

You should instantiate your objects in the constructor so you avoid null objects, example:

您应该在构造函数中实例化对象,以避免出现null对象,例如:

public class Event{
     public Event(){
          Instructor = new Instructor();
     }
}

#4


0  

Another approach I though of which is a debatable amount of nasty is:

另一种方法虽然是令人讨厌的令人讨厌的数量是:

@{try{@ev.Event.Instructor.Name}catch{}}

I decided to use this approach instead of adding 18 or so extra properties in the select all with ternary expressions.

我决定使用这种方法,而不是在使用三元表达式的select all中添加18个左右的额外属性。

#5


-1  

Something like:

@$ev.Event.Instructor != null ? @$ev.Event.Instructor.Name : String.Empty

#1


4  

One way is to add another property to Event:

一种方法是向Event添加另一个属性:

public string InstructorName
{
    get { return Instructor == null ? string.Empty : Instructor.Name; }
}

#2


3  

There is no such thing as "ignoring exception". You should not cause exceptions or should handle them.

没有“忽略例外”这样的事情。您不应该导致异常或应该处理它们。

public class Event {
     public Event() {
          this.Instructor = new Instructor();
     }
}

or

@(ev.Event.Instructor == null ? String.Empty : ev.Event.Instructor.Name)

#3


0  

You should instantiate your objects in the constructor so you avoid null objects, example:

您应该在构造函数中实例化对象,以避免出现null对象,例如:

public class Event{
     public Event(){
          Instructor = new Instructor();
     }
}

#4


0  

Another approach I though of which is a debatable amount of nasty is:

另一种方法虽然是令人讨厌的令人讨厌的数量是:

@{try{@ev.Event.Instructor.Name}catch{}}

I decided to use this approach instead of adding 18 or so extra properties in the select all with ternary expressions.

我决定使用这种方法,而不是在使用三元表达式的select all中添加18个左右的额外属性。

#5


-1  

Something like:

@$ev.Event.Instructor != null ? @$ev.Event.Instructor.Name : String.Empty