首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

使用“new”关键字创建对象实例解决方法

2012-05-11 
使用“new”关键字创建对象实例protected string getPidTitle(int pid){string outTxt outTxt new CB

使用“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,

所以你要改成

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属性了,所以出错了,解决办法就是参考我前面的回答

热点排行