protected string getPidTitle(int pid)
        {
            string outTxt = "";
            outTxt = new CBD.BLL.sroot().GetModel(pid).title;
            return outTxt;
        }
这段的错误提示是:使用“new”关键字创建对象实例。
在调用方法前通过检查确定对象是否为null。这个怎么改啊?
谢谢大家

解决方案 »

  1.   

    string能这样用吗?   我从没这样用过额
      

  2.   

    看清楚提示string outTxt = ""; 
                outTxt = new CBD.BLL.sroot().GetModel(pid).title; 
                return outTxt; 因为你的GeModel(pid)有可能为null,所以你要改成
    string outTxt = ""; 
                Object obj= new CBD.BLL.sroot().GetModel(pid);
    if(obj!=null)
    {
    outTxt =obj.title; 
    }
                return outTxt; 上面的Object改成你的类名就行
      

  3.   

    string 不用new。
    可用以下写法:
    protected string getPidTitle(int pid) 
            { 
                string outTxt = ""; 
                outTxt = CBD.BLL.sroot().GetModel(pid).title.ToString().Trim(); 
                return outTxt; 
            } 
      

  4.   

    string 不需要new啊  这个对象是内置弄好的  
      

  5.   

    string outTxt = ""; 
                Object obj= new CBD.BLL.sroot().GetModel(pid);
    if(obj!=null)
    {
    outTxt =obj.title; 
    }
                return outTxt; 
      

  6.   

    应该是这样: outTxt = new CBD.BLL().sroot().GetModel(pid).title; 你要先创建CBD.BLL对象
      

  7.   

    汗...语法问题啊!!!outTxt = new CBD.BLL.sroot().GetModel(pid).title; 
    修改为:
    outTxt = (new CBD.BLL.sroot()).GetModel(pid).title; 
      

  8.   

    outTxt = new CBD.BLL.sroot().GetModel(pid).title; 跟
    outTxt = (new CBD.BLL.sroot()).GetModel(pid).title;语法上是一样的,自己在VS中一打就知道了因为楼主获取的对像GetModel(pid)这个方式,不一定能够返回对像,如果返回null,如果是null就没有对像.title属性了,所以出错了,解决办法就是参考我前面的回答
      

  9.   

    string outTxt = ""; 
                Object obj= new CBD.BLL.sroot().GetModel(pid);
    if(obj!=null)
    {
    outTxt =obj.title; 
    }
                return outTxt; 
      

  10.   

    string outTxt=new  CBD.BLL.sroot().GetModel(pid);