NULL string comparison

 

Hi,

I've read https://www.mql5.com/en/forum/46695, but I still don't know how to test, if a string == NULL.

The following results in false:

string Str;
string NullStr;
      
Str = NULL;

Print(Str == NullStr);

So what's the solution?

Thanx.

 
mt4forum:

Hi,

I've read https://www.mql5.com/en/forum/46695, but I still don't know how to test, if a string == NULL.

The following results in false:

So what's the solution?

Thanx.

NULL is an int equalling 0

try this . . .

string Str;
string NullStr;
      
Str  = "";

if(Str == NullStr) Print("True !!");
else Print("False !!");
 
Don't create null strings, always initialize them (even if just to empty string.)
 
WHRoeder:
Don't create null strings, always initialize them.

The sentence is misleading. As far as I noticed there are no null strings in MQL. There are only null-terminated strings. Every variable in MQL is initiated (to its default), even if it is declared. Moreover, in the case the Str was explicitly set to "0", while the NullStr was initialized implicitly to "".

 
Then what are
ERR_NOT_INITIALIZED_STRING4008Not initialized string.
ERR_NOT_INITIALIZED_ARRAYSTRING4009Not initialized string in array.
An empty string ("") is not the same as a null string (uninitialized) Try running RaptorUK's test.
 

Maybe my problem get's clearer, when I describe, why I need the NULL string comparison:

I've programmed a functional layer around the timeseries functions (iClose, iOpen, iBarShift, ...).

This layer identifies, if the current chart is an offline chart or not and depending on that either calls the timeseries functions above or

my own functions, which directly operate on the history files.

Since all the timeseries functions allow to submit NULL as the parameter for symbol,

I need a way to check, if the parameter is NULL, in order to replace it by symbol() when calling my own functions.

 
mt4forum:

Maybe my problem get's clearer, when I describe, why I need the NULL string comparison:

I've programmed a functional layer around the timeseries functions (iClose, iOpen, iBarShift, ...).

This layer identifies, if the current chart is an offline chart or not and depending on that either calls the timeseries functions above or

my own functions, which directly operate on the history files.

Since all the timeseries functions allow to submit NULL as the parameter for symbol,

I need a way to check, if the parameter is NULL, in order to replace it by symbol() when calling my own functions.

The parameter that you use in the function call ? why do you need to check ? it's you that sets it.

Can you give an example of how/why you need to check this . . .

 

I don't understand your "checking iClose etc for an offline chart" The EA is running on the current chart. It can't tell the difference. If you call iClose(NULL,0,x) or Close[x] you get whatever the chart is.

 

@RaptorUK: I know that I can circumvent the problem. But I just want my functional layer to behave the same way as the timeseries functions,

so I want to call e.g. HstiClose(NULL, 0, shift).

@WHRoeder: MarketInfo(Symbol(), MODE_TICKSIZE) tells me, if the chart is an offline chart or not.

But let's not further dive into my intentions.

Is there a way to check, if Str == NULL?

It would be a strange thing in MQL allowing the assignment Str = NULL without providing the option to check for something like Str == NULL.

 
mt4forum:

@RaptorUK: I know that I can circumvent the problem. But I just want my functional layer to behave the same way as the timeseries functions,

so I want to call e.g. HstiClose(NULL, 0, shift).

@WHRoeder: MarketInfo(Symbol(), MODE_TICKSIZE) tells me, if the chart is an offline chart or not.

But let's not further dive into my intentions.

Is there a way to check, if Str == NULL?

It would be a strange thing in MQL allowing the assignment Str = NULL without providing the option to check for something like Str == NULL.

Did you understand what I meant when I wrote . . .

RaptorUK:

NULL is an int equalling 0

. . . NULL is not a string, as you can see in the examples about Type Casting


You can do what you want but it's up to you to handle what happens when you pass the integer value of NULL to your function . . . for example:

Files:
 

I am also confused about the coding part, if it is initializing zero then how could it get the proper answer.


html5 charts

Reason: