String References in functions

 
Hi there,

Strings are a bit quirky in MQL and I am struggling with a bit of code that employs string references as parameters to functions.

Rather than go into detail about the code in question, here is a simple example script that highlights the problem.


////////////////////////////////////////////////////////////////////////////

string StringCopy1(string &dest, string source) {
dest = source;
Print("StringCopy1 dest=["+dest+"]");
return (dest);
}

string StringCopy2(string &dest, string source) {
dest = source+"";
Print("StringCopy2 dest=["+dest+"]");
return (dest);
}

string StringCopy3(string &dest, string source) {
dest = StringConcatenate(source,"");
Print("StringCopy3 dest=["+dest+"]");
return (dest);
}


////////////////////////////////////////////////////////////////

int init() {
return (0);
}

////////////////////////////////////////////////////////////////

int deinit() {
return (0);
}

////////////////////////////////////////////////////////////////

int start() {

string a;

StringCopy1(a, "1111111111");
Print("StringCopy1 a=["+a+"]");

StringCopy2(a, "2222222222");
Print("StringCopy2 a=["+a+"]");

StringCopy3(a, "3333333333");
Print("StringCopy3 a=["+a+"]");

return(0);
}
////////////////////////////////////////////////////////////////


The script contains three functions StringCopy1, StringCopy2 & StringCopy3 that each perform the same task in so far as they copy one string to another. Each function does the copy in a slightly different way.

If you run this code as a script you will get the following output...

2008.06.10 12:12:07 strings EURUSDm,M15: removed
2008.06.10 12:12:07 strings EURUSDm,M15: uninit reason 0
2008.06.10 12:12:07 strings EURUSDm,M15: deinitialized
2008.06.10 12:12:07 strings EURUSDm,M15: StringCopy3 a=[3333333333]
2008.06.10 12:12:07 strings EURUSDm,M15: StringCopy3 dest=[3333333333]
2008.06.10 12:12:07 strings EURUSDm,M15: StringCopy2 a=[2222222222]
2008.06.10 12:12:07 strings EURUSDm,M15: StringCopy2 dest=[2222222222]
2008.06.10 12:12:07 strings EURUSDm,M15: StringCopy1 a=[1111111111]
2008.06.10 12:12:07 strings EURUSDm,M15: StringCopy1 dest=[1111111111]
2008.06.10 12:12:07 strings EURUSDm,M15: initialized
2008.06.10 12:12:07 strings EURUSDm,M15: loaded successfully


Which is what one would expect to see.

However if you comment out the "Print" lines within StringCopy1 StringCopy2 and StringCopy3 the StringCopy3 function no longer works and you get the following output.

2008.06.10 12:11:29 strings EURUSDm,M15: removed
2008.06.10 12:11:29 strings EURUSDm,M15: uninit reason 0
2008.06.10 12:11:29 strings EURUSDm,M15: deinitialized
2008.06.10 12:11:29 strings EURUSDm,M15: StringCopy3 a=[2222222222] **** WRONG SHOULD BE 3333333333
2008.06.10 12:11:29 strings EURUSDm,M15: StringCopy2 a=[2222222222]
2008.06.10 12:11:29 strings EURUSDm,M15: StringCopy1 a=[1111111111]
2008.06.10 12:11:29 strings EURUSDm,M15: initialized

Notice how the array passed to StringCopy3 which should contain "3333333333" does not.

Remove the comment from the Print lines in StringCopy3 and you get the following output...

2008.06.10 12:23:42 strings EURUSDm,M15: removed
2008.06.10 12:23:42 strings EURUSDm,M15: uninit reason 0
2008.06.10 12:23:42 strings EURUSDm,M15: deinitialized
2008.06.10 12:23:42 strings EURUSDm,M15: StringCopy3 a=[3333333333] *** CORRECT
2008.06.10 12:23:42 strings EURUSDm,M15: StringCopy3 dest=[3333333333]
2008.06.10 12:23:42 strings EURUSDm,M15: StringCopy2 a=[2222222222]
2008.06.10 12:23:42 strings EURUSDm,M15: StringCopy1 a=[1111111111]
2008.06.10 12:23:42 strings EURUSDm,M15: initialized
2008.06.10 12:23:42 strings EURUSDm,M15: loaded successfully

I am completely baffled as to why this happens. It really does make you question every single line of code you have written for MT4.

Can any MQL guru out there shed any light on why..

string StringCopy3(string &dest, string source) {
dest = StringConcatenate(source,"");
Print("StringCopy3 dest=["+dest+"]");
return (dest);
}

...works fine, but...

string StringCopy3(string &dest, string source) {
dest = StringConcatenate(source,"");
// Print("StringCopy3 dest=["+dest+"]");
return (dest);
}

...does not.

Thanks in advance,

Laurence.