【问题标题】:method can't return value, Error: can not convert type in C#, ASP.NET方法无法返回值,错误:无法在 C#、ASP.NET 中转换类型
【发布时间】:2011-06-22 15:10:44
【问题描述】:

我有一个应该返回一组数据的方法。但最后返回数据时,错误显示如下:“无法将类型'WebApplication1.Webrefernce13.DT_value[]'转换为'WebApplication1.Webrefernce13.DT_value”

public class InputHelp
{
    public DT_Value Priority()
    {
        WebReference13.DT_SM_InputHelp_Request IncomingtypeReq = new WebReference13.DT_SM_InputHelp_Request();

        WebReference13.DT_SM_InputHelp IncomingTypeResp;
        WebReference13.SI_CreateSM_OBService _proxy1 = new WebReference13.SI_CreateSM_OBService();
        CookieContainer cookie = new CookieContainer();
        _proxy1.Credentials = new NetworkCredential("xxxx", "xxxxx"); // use credential to acess to the SAP system
        _proxy1.CookieContainer = cookie;

        IncomingtypeReq.Mode = "Create";
        IncomingtypeReq.Language = "EN";

        IncomingtypeReq.OptionalText1 = "ZLFN";

        IncomingtypeReq.OptionalText2 = "";
        IncomingtypeReq.WSCallID = "223424dgdf";

        IncomingTypeResp = _proxy1.SI_GetInputHelp(IncomingtypeReq);

        DT_Value[] ab=new DT_Value[10];

        ab= IncomingTypeResp.Priority;

        return ab;  // error is here
    }

如果您能在这个问题上帮助我,我将不胜感激。

【问题讨论】:

    标签: c# asp.net web-services soap


    【解决方案1】:

    将您的方法签名更改为

    public DT_Value[] Priority(){...}
    

    还有

        DT_Value[] ab=new DT_Value[10];
        ab = IncomingTypeResp.Priority; //<-- something doesn't look right here.
    

    如果IncomingTypeResp.PriorityDT_Value[] 类型,那么你可以这样做

     DT_Value[] ab = IncomingTypeResp.Priority;
    

    【讨论】:

    • 嗨巴拉,非常感谢,它工作正常!!还有一个问题,如果您查看代码,我正在调用 Web 服务。但是,如果我再创建一种方法,那么我将在方法范围内再次调用 Web 服务。但我想最小化网络服务。介意说我该怎么做? /保罗
    【解决方案2】:

    您已将ab 声明为DT_Value 的数组,但该方法返回DT_Value(单个DT_Value 实例)。

    如果您希望 Priority 返回一个数组,您应该将声明更改为:

    public DT_Value[] Priority()
    

    【讨论】:

    • @Paul,如果我已经回答了您的问题,请将其标记为答案(单击答案旁边的绿色勾号)。谢谢。
    【解决方案3】:

    您将 ab 定义为 DT_Value 数组,但方法协定只返回一个 DT_Value。如果您只想返回第一个值(如果存在),请在最后一行

    return ab[0];
    

    【讨论】:

      【解决方案4】:

      如下声明,可能会对您有所帮助:

      DT_Value ab=new DT_Value;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        • 2017-05-21
        • 1970-01-01
        • 1970-01-01
        • 2017-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多