accessing LocalStorage in a static method

Created at 27 May 2024, 21:48
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AlgoCreators's avatar

AlgoCreators

Joined 16.01.2022

accessing LocalStorage in a static method
27 May 2024, 21:48


hi my friends

I am facing an issue with accessing LocalStorage in a static method. I understand that LocalStorage.GetString cannot be accessed in a static context. Could you please guide me on the correct approach to access LocalStorage within a static method, or suggest an alternative solution?

Thank you

this is my sample code:

public class sample
{
    public static bool Check()
    {
        string MyLocalStorage = LocalStorage.GetString("Data", LocalStorageScope.Type);
        if (MyLocalStorage == null)
        {
            return false;
        }
        else
        {
            return true;
     
        }
    }
}

@AlgoCreators
Replies

PanagiotisCharalampous
28 May 2024, 06:07

Here you go

        public static bool Check(Robot robot)
        {
            string MyLocalStorage = robot.LocalStorage.GetString("Data", LocalStorageScope.Type);
            if (MyLocalStorage == null)
            {
                return false;
            }
            else
            {
                return true;
         
            }
        }

@PanagiotisCharalampous

AlgoCreators
28 May 2024, 06:27

Great, thank you very much


@AlgoCreators

AlgoCreators
28 May 2024, 16:09

RE: accessing LocalStorage in a static method

PanagiotisCharalampous said: 

Here you go

        public static bool Check(Robot robot)        {            string MyLocalStorage = robot.LocalStorage.GetString("Data", LocalStorageScope.Type);            if (MyLocalStorage == null)            {                return false;            }            else            {                return true;                     }        }

Dear friend, unfortunately, I could not call it correctly and with the error:
Crashed in Initialize with NullReferenceException: Object reference not set to an instance of an object.

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class test_indi : Indicator
    {
        public static bool Check(Indicator indicator)
        {
            string MyLocalStorage = indicator.LocalStorage.GetString("Data", LocalStorageScope.Type);
            if (MyLocalStorage == null)
            {
                return false;
            }
            else
            {
                return true;
         
            }
        }
        protected override void Initialize()
        {
        Indicator indicator=new test_indi();
        Print(Check(indicator));
        }

        public override void Calculate(int index)
        {
        
        }
    }
}

 

 


@AlgoCreators

AlgoCreators
28 May 2024, 16:13 ( Updated at: 28 May 2024, 16:16 )

The problem was solved!
I was creating a new object by mistake when there was no need to do this and I should have used "this".

Print(Check(this));

thank you.


@AlgoCreators