ArrayCopy won't work on one program, works on another

 

EDIT: PROBLEM SOLVED. Can't use ArrayCopy with struct type if one of the struct members is of the string data type. Reason in red.

I'm following the same template for copying two arrays of the struct type. I use one on an include file that's part of an EA, which works (and also worked as part of the EA before I transferred the code to an include file), and the one I'm using in an indicator doesn't work. Not sure if that's related, but I thought I'd mentioned it anyway. Other than that, the only difference is that the one that works uses various data types as members, while the one that doesn't work uses only the string type as members.

The first code works, the second code doesn't copy the array.

I looked at the help manual and I found this:

Array of classes and structures containing objects that require initialization aren't copied. An array of structures can be copied into an array of the same type only.

For static and dynamic arrays (except for class and structure members), the size of a destination array is automatically increased to the amount of copied data (if the latter exceeds the array size).

Is it the reason in red? I also added int, double, and datetime as members to the struct of the problem code, initialized the member variables for all elements, and it still doesn't doesn't copy the array.

And if it was the reason in blue, why would it work in the first code but not in the second code?

Like I mentioned, the biggest difference I can see is that the working code is in an include file that's part of an EA while the problem code is part of an indicator.

If someone can clarify what exactly is happening here, I'd appreciate it.


//--- Global Scope

struct Data{ int ticket,direction; double entryPrice,lots,SL,TP; datetime entryTime; };

Data orderArray[]; 

//---

void resizeAfterClose(int i){
   Data tempArray[];
   ArrayCopy(tempArray,orderArray);
   int count=0;
   ArrayResize(orderArray,0);

   for(int x=0; x<=ArraySize(tempArray)-1; x++){
      if(x!=i){
         ArrayResize(orderArray,count+1);

         orderArray[count].ticket       = tempArray[x].ticket;
         orderArray[count].direction    = tempArray[x].direction;
         orderArray[count].entryPrice   = tempArray[x].entryPrice;
         orderArray[count].lots         = tempArray[x].lots;
         orderArray[count].SL           = tempArray[x].SL;
         orderArray[count].TP           = tempArray[x].TP;
         orderArray[count].entryTime    = tempArray[x].entryTime;
         
         count++;
      }
   }   
   currentDB = OrdersTotal();
   symbolDB = ArraySize(orderArray);
}


//--- Global Scope
struct Data { string title; string type; string country; string date; string time;
               string timeRemain; string impact; string previous; string forecast; string actual; };

Data calEvents[];
//---


void delEvent(int x){
   Data tempArray[];
   int count = 0;   

   ArrayCopy(tempArray,calEvents);
   ArrayResize(calEvents,0);

   for (int i = 0; i <= ArraySize(tempArray)-1; i++){
      if (i != x){
         ArrayResize(calEvents,count+1);
         
         calEvents[count].title      = tempArray[i].title;
         calEvents[count].type       = tempArray[i].type;
         calEvents[count].country    = tempArray[i].country;
         calEvents[count].date       = tempArray[i].date;
         calEvents[count].time       = tempArray[i].time;
         calEvents[count].timeRemain = tempArray[i].timeRemain;
         calEvents[count].impact     = tempArray[i].impact;
         calEvents[count].previous   = tempArray[i].previous;
         calEvents[count].forecast   = tempArray[i].forecast;
         calEvents[count].actual     = tempArray[i].actual;

         count++;
      }      
   }
}
 
user3822:

I'm following the same template for copying two arrays of the struct type. I use one on an include file that's part of an EA, which works (and also worked as part of the EA before I transferred the code to an include file), and the one I'm using in an indicator doesn't work. Not sure if that's related, but I thought I'd mentioned it anyway. Other than that, the only difference is that the one that works uses various data types as members, while the one that doesn't work uses only the string type as members.

The first code works, the second code doesn't copy the array.

I looked at the help manual and I found this:

Is it the reason in red? I also added int, double, and datetime as members to the struct of the problem code, initialized the member variables for all elements, and it still doesn't doesn't copy the array.

And if it was the reason in blue, why would it work in the first code but not in the second code?

Like I mentioned, the biggest difference I can see is that the working code is in an include file that's part of an EA while the problem code is part of an indicator.

If someone can clarify what exactly is happening here, I'd appreciate it.




The strings need initialization. You may use ushort (or uchar) arrays instead.

 
Ex Ovo Omnia:


The strings need initialization. You may use ushort (or uchar) arrays instead.

LOL just figured it out right now by creating another program without a string type as a struct member.

Thanks for the solution, btw. I was wondering how would I go about this.

I'll read over the differences between ushort and uchar. Which would you recommend?

 
Why are you using ArrayCopy?
Struct requires initialization, create an constructor, so you can have an array of them.
And add an assignment operator so it copies itself.
struct Data { string title; string type; string country; string date; string time;
               string timeRemain; string impact; string previous; string forecast; string actual; 
  void Data(void) : title(""), type(""), country(""), date(""), time(""),
               timeRemain(""), impact(""), previous(""), forecast(""), actual("")
               {}
  void operator=(const Data& that){
    title=that.title; type=that.type; country=that.country; date=that.date, time=that.time;
               timeRemain=that.timeRemain; impact=that.impact; previous=that.previous; 
               forecast=that.forecast; actual=that.actual;
               }
};
Just enlarge the array and initialize the new entry
ArrayResize(calEvents,ArraySize(calEvents)+1);
Just move higher elements down and shrink the array.
void delEvent(int x){
   int last = ArraySize(calEvents) - 1;
   while(x < last){
      calEvents[x] = calEvents[x+1]; ++x;
   }
   ArrayResize(calEvents, last);
}
Reason: