0

I've been looking up static methods and non-static properties but I don't understand how it applies here. It's been a while since I've looked at C# code. I get the following error message from the following code. Any help would be greatly appreciated.

(awaitable) Task > IgRestApiClient.createPositionV2(CreatePositionRequest createPositionRequest) Creates an OTC position @param createPositionRequest the request for creating a position @return OTC create position response An object reference is required for the non-static field, method, or property 'IgRestApiClient.createPositionV2 (CreatePositionRequest)'

public async void BuyThis()
{
    try
    {
        var buyDataX = new CreatePositionRequest
        {
            epic = "THIS",
            expiry = "MAY-20",
            direction = "SELL",
            size = 1,
            level = 100,
            orderType = "LIMIT",
            guaranteedStop = false,
            stopLevel = null,
            stopDistance = null,
            trailingStop = false,
            trailingStopIncrement = null,
            forceOpen = false,
            limitLevel = null,
            limitDistance = null,
            quoteId = null,
            currencyCode = "USD",
            timeInForce = "EXECUTE_AND_ELIMINATE",
        };

        var response = await IgRestApiClient.createPositionV2(buyDataX);
    }

    catch (Exception ex)
    {

    }
}

public async Task<IgResponse<CreatePositionResponse>> createPositionV2(dto.endpoint.positions.create.otc.v2.CreatePositionRequest createPositionRequest)
        {
            return await _igRestService.RestfulService<CreatePositionResponse>("/gateway/deal/positions/otc", HttpMethod.Post, "2", _conversationContext, createPositionRequest);
        }
petedotg
  • 119
  • 2
  • 4
  • 10

3 Answers3

1
var response = await IgRestApiClient.createPositionV2(buyDataX);

IgRestApiClient is not an object, but a class definition, so it's telling you that you are trying to use a non static method as if it was static.

Initiate the IgRestApiClient with new IgRestApiClient(), and then use that object to call the createPositionV2.

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Erndob
  • 2,512
  • 20
  • 32
1

You've got this non static class:

public class IgRestApiClient{

    public async Task<IgResponse<CreatePositionResponse>> createPositionV2(CreatePositionRequest createPositionRequest)
    {
        return await _igRestService.RestfulService<CreatePositionResponse>("/gateway/deal/positions/otc", HttpMethod.Post, "2", _conversationContext, createPositionRequest);
    }
}

You're trying to use it statically:

var response = await IgRestApiClient.createPositionV2(buyDataX);

You either need this (make class static):

public static class IgRestApiClient{

Or this (create an instance first):

var x = new IgRestApiClient();
var response = await x.createPositionV2(buyDataX);

Which one to pick? Well.. whichever one is used elsewhere. If this is the only use, I guess you need to decide based on how IgRestApiClient looks like its lifecycle should behave. Perhaps it reuses something properly and hence looks like it should be single instance, or perhaps it disposes something after a single use and hence relies on being renewed every time it is to be used. (I get the feeling this isn't your code originally that you're maintaining)


Incidentally, the method signature will get simpler if you using dto.endpoint.positions.create.otc.v2, if you didn't already- then you can just refer to CreatePositionRequest

Also, you may be able to declare this method like:

    public Task<IgResponse<CreatePositionResponse>> createPositionV2Async(CreatePositionRequest createPositionRequest)
    {
        return _igRestService.RestfulService<CreatePositionResponse>("/gateway/deal/positions/otc", HttpMethod.Post, "2", _conversationContext, createPositionRequest);
    }
}

There may be no need to use async/await here because the method doesn't do anything other than make a call and get a task and pass it on. This doesn't mean it's no longer async in the way it behaves; it returns a task, it's asynchronous, and I've renamed it to highlight this, but if you have methods that don't do anything with tasks other than return await them then you may be able to just return the Task that the other method created and let the calling method await it just the same.

Take a read of What is the purpose of "return await" in C#? for some interesting discussion around the concept

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

You need to initialize a object & then only can call the methods of non static class. As I can see IgRestApiClientis not not staticso you need to initialise an instance of class before calling it.

IgRestApiClient _igRestApiClient = new IgRestApiClient();

Then you can call it like

var response = await _igRestApiClient.createPositionV2(buyDataX);

for better solution with dependency injection you can do it like :

public partial class IgRestApiClient
    {
        private PropertyEventDispatcher eventDispatcher;
        private ConversationContext _conversationContext;

        private IgRestService _igRestService;

        public IgRestApiClient(string environment, PropertyEventDispatcher eventDispatcher)
        {
            this.eventDispatcher = eventDispatcher;
            this._igRestService = new IgRestService(eventDispatcher, environment);
        }




    ///<Summary>
    ///Creates an OTC position
    ///@param createPositionRequest the request for creating a position
    ///@return OTC create position response
    ///</Summary>

    public async Task<IgResponse<CreatePositionResponse>> createPositionV2(dto.endpoint.positions.create.otc.v2.CreatePositionRequest createPositionRequest)
    {
        return await _igRestService.RestfulService<CreatePositionResponse>("/gateway/deal/positions/otc", HttpMethod.Post, "2", _conversationContext, createPositionRequest);
    }

For reference :

https://github.com/IG-Group/ig-webapi-dotnet-sample/blob/master/IGWebApiClient/IGRestApiClient.cs

https://csharp.hotexamples.com/site/file?hash=0x52fbb58774cf38505f1f8d4da108670fffda8578a1b7dcb434a671907f5ddaab&fullName=IGApi/IGApiClientApplication.cs&project=JBetser/MiDax

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104