accessing LocalStorage in a static method
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;
}
}
}
Replies
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
PanagiotisCharalampous
28 May 2024, 06:07
Here you go
@PanagiotisCharalampous