【问题标题】:cannot assign <null> to implicit types local variable using asp.net无法使用 asp.net 将 <null> 分配给隐式类型局部变量
【发布时间】:2010-06-18 01:29:23
【问题描述】:

我有这个

var  result = general.GetInformation(int.Parse(ID), F_account, F_Info, Types);

这个 GetInformation 是我的 Entity.Getinformation 类。当我尝试全局分配结果时,我无法分配给隐式类型的局部变量?

var result = ?

我应该在全局分配什么?

谢谢

【问题讨论】:

    标签: c# asp.net implicit-typing


    【解决方案1】:

    听起来您正在尝试执行 var result = null; 这不起作用,因为 null 没有告诉编译器 result 应该是什么类型。您需要使用Sometype result = null;。

    【讨论】:

    • 感谢您,正如您所提到的:这是 错误 没有类型:var studentDetails = null; 但这是 正确 类型 List&lt;StudentListVM&gt; studentDetails = null; ,希望对某人有所帮助。
    【解决方案2】:

    当您说“全局分配结果”时,您是指将其用作类变量吗?

    class SomeClass {
        var result = general.GetInformation(int.Parse(ID), F_account, F_Info, Types);
    }
    

    在这种情况下,you can't use var,您将不得不使用 GetInformation 返回的任何类型,例如

    string result =  general.GetInformation(int.Parse(ID), F_account, F_Info, Types);
    

    或

    Entity result =  general.GetInformation(int.Parse(ID), F_account, F_Info, Types);
    

    【讨论】:

      【解决方案3】:

      您的错误是否类似于“无法将方法组分配给隐式类型的局部变量”?

      另外,GetInformation 是否是一个类?

      如果这两个是正确的,那么问题是你试图对方法名称使用隐式类型,var 不允许这样做。

      【讨论】:

        【解决方案4】:

        你可以像下面这样使用:

        因为你的class是:Getinformation

        然后

        Getinformation result =null;
        
        result = general.GetInformation(int.Parse(ID), F_account, F_Info, Types);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多