CharToString Question

 

I'm getting a compilation error when trying to set a string variable to equal a ing variable.


Code:    string mytest = CharToStr(32);   


Error: 'CharToStr' - initialization expected


Since CharToString() is a built in fuction, I don't understand why this statement would upset the compiler.


Any clues or workarounds would be appreciated :)


Regards,

Merlin


 
I tested your string, no problems. Try to reset your computer.
 
Error: 'CharToStr' - initialization expected

Merlin,

This kind of error makes me think that you're placing the command in the wrong place in your code. This kind of statement must be located inside a function i.e. start(), init(), deinit(), or a user function. You can't put it in your declarations at the top.

- Tovan

 
tovan wrote >>

Merlin,

This kind of error makes me think that you're placing the command in the wrong place in your code. This kind of statement must be located inside a function i.e. start(), init(), deinit(), or a user function. You can't put it in your declarations at the top.

- Tovan

Thanks for the responses.

Interesting, the difference between them..... Roger says it works, and Tovan says it can't :)

I don't see why you can't declare a String to = a String, which is what CharToString is.

But, since there's no problem with me moving that bunch of code to the Init section, I suspect the problem is solved and I thank you both.

My personal opinion is that whomever created the MQL compiler was a brilliant math geek and just didn't "get" the power of strings. There are missing *BASIC* string manipulation tools in the language. Still..... there's usually a workaround.

Best,
Merlin

 
MerlinBrasil wrote >>

Thanks for the responses.

Interesting, the difference between them..... Roger says it works, and Tovan says it can't :)

I don't disagree with Roger. The code works if you put it in the correct place. I just made an assumption based on my own personal experiences that turned out to be right as it seems. I've made the same mistakes in the past so I've seen it before. Live and learn...

- Tovan

 
tovan wrote >>

I don't disagree with Roger. The code works if you put it in the correct place. I just made an assumption based on my own personal experiences that turned out to be right as it seems. I've made the same mistakes in the past so I've seen it before. Live and learn...

- Tovan

Thanks, Tovan :)

It appears to now be working. My question is why a simple string assignment can't be global using CharToString. I have 34 such strings and it would be most efficient if they were only assigned 'once at the top'.

But, as in life, we play the cards we're dealt :)

Best,

Merlin

 
MerlinBrasil wrote >>

Thanks, Tovan :)

It appears to now be working. My question is why a simple string assignment can't be global using CharToString. I have 34 such strings and it would be most efficient if they were only assigned 'once at the top'.

But, as in life, we play the cards we're dealt :)

Best,

Merlin

Is not a 'simple string assignment', is runtime call to builtin function, is what compiler sees. That's the issue. If look at MQL4 ref. https://docs.mql4.com/basis/variables/static you can see. Only same data type constants can be used at global scope - ie, static's

The pack in this case is indeed ltd...

Regards

 
fbj:

Is not a 'simple string assignment', is runtime call to builtin function, is what compiler sees. That's the issue. If look at MQL4 ref. https://docs.mql4.com/basis/variables/static you can see. Only same data type constants can be used at global scope - ie, static's

The pack in this case is indeed ltd...

Regards


Thanks, fbj. I'll see if I can optimize the code with that in mind.


Good people here.... good advice, all :)


Best,

Merlin

 

Merlin,

As an aside or maybe sometime suit your purpose: #define is worthy of investigation.

A very useful compile time language element to have in your tool bag.

If optimisation is seen as an issue in your code then they can decrease runtime overheads. But I sometimes wonder about being preoccupied with runtime speed. Might sound confrontational, but IF once's strategy is that touchy regards speed, perhaps is not correct strategy ;)

The interpreter is no slouch and a compiled to native code mql5 program will only get faster!

eg, why use identifiers that use memory and memory references [+whatever else interpreter gets up to when handling identifiers that are memory mapped :] IF just want a constant value?

no call to a function to convert eg, 32 to " " because you did eg, #define MYTEST " "

fwiw...

 
fbj:

Merlin,

As an aside or maybe sometime suit your purpose: #define is worthy of investigation.

A very useful compile time language element to have in your tool bag.

If optimisation is seen as an issue in your code then they can decrease runtime overheads. But I sometimes wonder about being preoccupied with runtime speed. Might sound confrontational, but IF once's strategy is that touchy regards speed, perhaps is not correct strategy ;)

The interpreter is no slouch and a compiled to native code mql5 program will only get faster!

eg, why use identifiers that use memory and memory references [+whatever else interpreter gets up to when handling identifiers that are memory mapped :] IF just want a constant value?

no call to a function to convert eg, 32 to " " because you did eg, #define MYTEST " "

fwiw...

Confrotational? No worries :) I very much appreciate another point of view and, being challenged, tends to bring out the best in others :)


Although I did like the graphical concept of using Wingdings to communicate certain info, I went back to the drawing board, did some new-concept thinking and came up with another way to go.


I still have one or two minor mysteries to solve, but will be shortly posting all the code here. It's actually running quite well, and greatly assisting profitable trading.


As for MT5, at this point I'm not interested. I resent the new anti-hedging rules and it's difficult enough for most traders to win without giving the brokers yet another advantage. IMO, of course. MT4, in general, suits me just fine :)


Thanks for your time and advice.


Best,

Merlin

 
string Chr(int ascii)
   {
   string res;
   res="0";
   res=StringSetChar(res,0,ascii);
   res=StringSubstr(res,0,1);
   return(res);
   }
Reason: