Variable struct names

 
This code doesn't work:

struct LockStruct {
   int ticket;
   int locked;
};

orderTicket1 = orderOpen("buy");
LockStruct orderTicket1 = {orderTicket1,1};

'orderTicket1' - constant expression required
declaration of 'orderTicket1' hides global variable

If I am interpreting this correctly, I cannot name a struct instance after the value of a variable (orderTicket1 which is declared in the global scope). Only "hard" names are allowed.

So I can't do this:
if (orderTicket1.locked == 1) {Print("It's locked!");}

Because I can only use "hard" names.

LockStruct SomeTicket = {
        ticket = orderTicket1;
        locked = 1
};

But then I can't perform lookups:

if (orderTicket1.locked == 1) {Print("It's locked!");}

Then how is a struct any better than:

SomeTicket_ticket = orderTicket1;
SomeTicket_locked = 1;

which won't let me perform lookups either?

Is there anything in MQL4 that remotely resembles an associative array?
 
whoowl:

It's not clear what you are trying to do.

What data type does orderOpen() return?

What is the data type of orderTicket1, which is declared in the global scope?

Why are you trying to create a local structure with an identifier that is already used for a global object? Do you need a new instance of the structure, or do you want to change the one that is declared at the global level?

 

Thank you for your reply.

orderOpen() returns an integer which is the order ticket.


Basically, I want this:

Array/Struct called Locks with two columns:

ticket          locked

254587896       1

587856963       1

458789563       0




Then look it up:

if (Locks.587856963 == 1) {Print("It's locked.");
 
whoowl #:
orderOpen() returns an integer which is the order ticket.

You can't assign int to a struct:

whoowl:
orderTicket1 = orderOpen("buy");

Here is a simple script to check this:

struct LockStruct
  {
   int ticket;
   int locked;
  };

void OnStart()
  {
   // Here I declared a structure and assigned some values to it. Because in your code the structure
   // already existed in the global scope and contained some values.
   LockStruct orderTicket1 = {2, 1};
   //---
   orderTicket1 = get123(); // Error: 'get123' - parameter passed as reference, variable expected
  }

int get123()
  {
   return(123);
  }
 
Vladislav Boyko #:
You can't assign int to a struct:

You can assign this to a struct member, but not to the struct:

struct LockStruct
  {
   int ticket;
   int locked;
  };

void OnStart()
  {
   LockStruct orderTicket1 = {2, 1};
   orderTicket1.ticket = get123(); // OK
  }

int get123() { return(123); }

This is also completely wrong:

whoowl:
LockStruct orderTicket1 = {orderTicket1,1};

Check out the documentation on structures:

https://www.mql5.com/en/docs/basis/types/classes

Despite the fact that this is MQL5 documentation, all the information in the link above applies to MQL4 too

Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
  • www.mql5.com
Structures, Classes and Interfaces - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
whoowl:
Is there anything in MQL4 that remotely resembles an associative array?

You can implement this or look for a ready-made implementation.

A Google query produces a large number of results, including articles: