method can't return value, Error: can not convert type in C#, ASP.NET

折月煮酒 提交于 2019-12-25 01:14:30

问题


I have method that should return a set of data. But at last when returning data then error shows like :"can not convert type 'WebApplication1.Webrefernce13.DT_value[]' to '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
    }

I will be grateful if you can help me on this issue.


回答1:


change your method signature to

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

also

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

if IncomingTypeResp.Priority is of type DT_Value[] then you can just do

 DT_Value[] ab = IncomingTypeResp.Priority;



回答2:


You've declared ab as an array of DT_Value, but the method returns DT_Value (a single DT_Value instance).

If you want Priority to return an array you should change the declaration to this:

public DT_Value[] Priority()



回答3:


You are defining ab as an array of DT_Value but the method contract is to return only one DT_Value. If you want to return only the first value (if it exists), make your last line

return ab[0];



回答4:


Declare like below and might help you:

DT_Value ab=new DT_Value;


来源:https://stackoverflow.com/questions/6442160/method-cant-return-value-error-can-not-convert-type-in-c-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!