使用“new”关键字创建对象实例解决方法
使用“new”关键字创建对象实例
protected string getPidTitle(int pid)
{
string outTxt = "";
outTxt = new CBD.BLL.sroot().GetModel(pid).title;
return outTxt;
}
这段的错误提示是:使用“new”关键字创建对象实例。
在调用方法前通过检查确定对象是否为null。
这个怎么改啊?
谢谢大家
------解决方案--------------------
看清楚提示
string outTxt = "";
outTxt = new CBD.BLL.sroot().GetModel(pid).title;
return outTxt;
因为你的GeModel(pid)有可能为null,
所以你要改成
protected string getPidTitle(int pid)
{
string outTxt = "";
outTxt = new CBD.BLL.sroot().GetModel(pid).title;
return outTxt;
}
这段的错误提示是:使用“new”关键字创建对象实例。
在调用方法前通过检查确定对象是否为null。
这个怎么改啊?
谢谢大家
------解决方案--------------------
看清楚提示
string outTxt = "";
outTxt = new CBD.BLL.sroot().GetModel(pid).title;
return outTxt;
因为你的GeModel(pid)有可能为null,
所以你要改成
- C# code
string outTxt = ""; Object obj= new CBD.BLL.sroot().GetModel(pid); if(obj!=null) { outTxt =obj.title; } return outTxt;
------解决方案--------------------
string 不用new。
可用以下写法:
protected string getPidTitle(int pid)
{
string outTxt = "";
outTxt = CBD.BLL.sroot().GetModel(pid).title.ToString().Trim();
return outTxt;
}
------解决方案--------------------
outTxt = new CBD.BLL.sroot().GetModel(pid).title;
跟
outTxt = (new CBD.BLL.sroot()).GetModel(pid).title;
语法上是一样的,自己在VS中一打就知道了
因为楼主获取的对像GetModel(pid)这个方式,不一定能够返回对像,如果返回null,如果是null就没有对像.title属性了,所以出错了,解决办法就是参考我前面的回答