将值设置为bool属性 [英] Setting a value to bool property

查看:111
本文介绍了将值设置为bool属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为bool值赋值



public bool Isoverhung = false;



我正在获得价值,比如



class.Isoverhung = _presenter.Getthevalue();



但是我得到null引用异常如何解决这个问题?

I am assigning value to a bool value like

public bool Isoverhung=false;

and i am fetching the value like

class.Isoverhung=_presenter.Getthevalue();

But i am getting null reference exception how can i solve this?

推荐答案

_presenter.Getthevalue();必须返回undefine值。

检查如convert.tobool(_presenter.Getthevalue())
_presenter.Getthevalue(); Must be return undefine Value.
Check like convert.tobool(_presenter.Getthevalue())


class.Isoverhung=_presenter.Getthevalue();



在上面的代码行中,类对象可以为null,或者_presenter可以为null 。如果两者都不为null,则Getthevalue方法中可以有null对象!

但是我们不能确切地说出哪个对象为空,或者你可以用给定的信息做什么,你可以放在上面的行之前指向并逐行检查导致此异常然后您可以找到解决方案。检查以下关于codeproject的问题以获取更多信息

[ C#] NullReference异常未处理 [ ^ ]


试试这个ternarry运营商



try this ternarry operator

bool outValue;
class.Isoverhung=bool.TryParse(_presenter.Getthevalue(),out outValue)?outValue:default(bool);


这篇关于将值设置为bool属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆