简体   繁体   中英

Generate a library and return responses

I have generated a class library, I want to return two responses.

Now I get an error:

A field initializer cannot reference the nonstatic field, method, or property

My questions:

  1. How to fix the error?
  2. How to return two responses? I have to reference it in another console application.

Code:

namespace MessagingConfiguration
{
    public class InmateDetail
    {
        public GetConfig(string Pin, int Id)
        { 
            AuthenticatedJsonServiceClient client = new AuthenticatedJsonServiceClient("http://www.test.my.com/MessageService/api/");
           InmateDetailResponse inmateDetail = client.Get(new InmateDetailRequest() { InmatePIN = Pin, FacilityId = Id });
           InmateInboxResponse inmateInbox = client.Get(new InmateInboxRequest() { InmatePkey = inmateDetail.Inmate.pkey.ToString() });
        }

     }

  }

Write a new model that will wrap the two existing models as properties:

public class MyModel
{
    public InmateDetailResponse InmateDetail { get; set; }
    public InmateInboxResponse InmateInbox { get; set; }
}

and then have your method return this new model:

namespace MessagingConfiguration
{
    public class InmateDetail
    {
        public MyModel GetConfig(string Pin, int Id)
        { 
            AuthenticatedJsonServiceClient client = new AuthenticatedJsonServiceClient("http://www.test.my.com/MessageService/api/");
            InmateDetailResponse inmateDetail = client.Get(new InmateDetailRequest() { InmatePIN = Pin, FacilityId = Id });
            InmateInboxResponse inmateInbox = client.Get(new InmateInboxRequest() { InmatePkey = inmateDetail.Inmate.pkey.ToString() });

            MyModel model = new MyModel();
            model.InmateDetail = inmateDetail;
            model.InmateInbox = inmateInbox;

            return model;
        }
    }
}

Now when you call the method you will have access to the two properties:

MyModel model = new InmateDetail().GetConfig("some pin", 123);
// here you can use model.InmateDetail and model.InmateInbox 

It seems like the incriminating line is this:

InmateDetailResponse inmateDetail = client.Get(new InmateDetailRequest() { InmatePIN = Pin, FacilityId = Id });

I would suggest you follow the standard .NET convention of naming your variables/parameters with a lower-case letter to start:

public GetConfig(string pin, int id)

This will make the incriminating line change to:

InmateDetailResponse inmateDetail = client.Get(new InmateDetailRequest() { InmatePIN = pin, FacilityId = id });

Without the code for InmateDetailRequest , I can only hope that it has the publically settable fields InmatePIN (should really be called InmatePin ) and FacilityId .

Try initializing the fields in a constructor.

namespace MessagingConfiguration
{
    public class InmateDetail
    {
        public InmateDetail(string Pin, int Id)
        { 
            AuthenticatedJsonServiceClient client = new AuthenticatedJsonServiceClient("http://www.test.my.com/MessageService/api/");
           InmateDetailResponse inmateDetail = client.Get(new InmateDetailRequest() { InmatePIN = Pin, FacilityId = Id });
           InmateInboxResponse inmateInbox = client.Get(new InmateInboxRequest() { InmatePkey = inmateDetail.Inmate.pkey.ToString() });
        }    
     }    
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM