QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: madscijr on December 22, 2020, 12:24:30 pm

Title: setting string constant to a chr$ returning type mismatch?
Post by: madscijr on December 22, 2020, 12:24:30 pm
I'm getting a "type mismatch" error on this line:
Code: QB64: [Select]
  1. CONST vbCrLf$ = chr$(10) + chr$(13)
  2.  

I can probably use a variable instead, but was just wondering why this is returning an error and if there is any error in my notation that could be slightly altered to make it work?
Thanks...
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: bplus on December 22, 2020, 12:45:47 pm
CONST doesn't handle string functions, I guess. It's a separate EVAL sub that Steve made to do CONSTs.

Work around is DIM SHARED that thing.
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: SpriggsySpriggs on December 22, 2020, 01:34:59 pm
@madscijr @bplus
I'd change this to a function so that it is similar to a CONST in that you aren't able to change the value at runtime. DIM SHARED won't prevent you from changing it accidentally.

Code: QB64: [Select]
  1. FUNCTION vbCrLf$
  2.   vbCrLf = CHR$(10) + CHR$(13)
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: bplus on December 22, 2020, 02:07:36 pm
Yeah that's another way!
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: madscijr on December 22, 2020, 06:45:13 pm
Thanks guys for your answers. I like the solution of using a function, it's simple but flexible.
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: Pete on December 22, 2020, 07:24:13 pm
I think coders overuse CONST. I seldom find a use for it.

Pete
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: SpriggsySpriggs on December 22, 2020, 07:24:32 pm
Thanks guys for your answers. I like the solution of using a function, it's simple but flexible.

You are very welcome. Glad to be of assistance.
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: TempodiBasic on December 24, 2020, 08:15:57 pm
Hi
just to be clear this code doen't work in Qbasic
Code: QB64: [Select]
  1. CONST vbCrLf$ = CHR$(10) + CHR$(13)
  2.  
as soon as you RUN the program you get an error runtime!
Moreover  this code takes no error
Code: QB64: [Select]
  1. CONST vbCrLf = CHR$(10) + CHR$(13)
  2.  
but as soon as you add this string to another string , you got the mismatch error!
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: zaadstra on January 02, 2021, 11:39:54 am
I'm getting a "type mismatch" error on this line:
Code: QB64: [Select]
  1. CONST vbCrLf$ = chr$(10) + chr$(13)
  2.  

I can probably use a variable instead, but was just wondering why this is returning an error and if there is any error in my notation that could be slightly altered to make it work?
Thanks...

I think the type mismatch is saying that you can't use (and don't need to) the $ sign here.  A CONST can contain strings or numbers.
It just seems that CONST does not like control characters in a string.   I never use CONST,  I just tested something, and peeked into the wiki: https://www.qb64.org/wiki/CONST (https://www.qb64.org/wiki/CONST)

Code: QB64: [Select]
  1. CONST crlf = CHR$(10) + CHR$(13)
  2. CONST cr = CHR$(13)
  3. CONST lf = CHR$(10)
  4. CONST blah = "hello"
  5. PRINT blah
  6. PRINT cr; lf
  7. PRINT "there"
  8.  
  9. Result:
  10. hello
  11. 0  0
  12. there
  13.  
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: bplus on January 02, 2021, 12:36:41 pm
I think the type mismatch is saying that you can't use (and don't need to) the $ sign here.  A CONST can contain strings or numbers.
It just seems that CONST does not like control characters in a string.   I never use CONST,  I just tested something, and peeked into the wiki: https://www.qb64.org/wiki/CONST (https://www.qb64.org/wiki/CONST)

Code: QB64: [Select]
  1. CONST crlf = CHR$(10) + CHR$(13)
  2. CONST cr = CHR$(13)
  3. CONST lf = CHR$(10)
  4. CONST blah = "hello"
  5. PRINT blah
  6. PRINT cr; lf
  7. PRINT "there"
  8.  
  9. Result:
  10. hello
  11. 0  0
  12. there
  13.  

Nope, still broken:
Code: QB64: [Select]
  1. CONST crlf = CHR$(10) + CHR$(13)
  2.    print "hello" + crlf + crlf  + crlf + crlf  + crlf + crlf  + crlf + crlf
  3.    print "world"
  4.  
  5.  
  6.  
Title: Re: setting string constant to a chr$ returning type mismatch?
Post by: SMcNeill on January 02, 2021, 06:31:10 pm
CONST has very limited native support for what it does.  When I wrote my math processor, I plugged it in to handle most math operations for us, but I never wrote it to process STRING manipulation.  We don't calculate CHR$, LEFT$, RIGHT$, MID$, STR$, or VAL type commands at all.  (At least, not as far as I'm aware.  If so, whatever it is is just a special case that Galleon accounted for back before version 0.9 when I expanded CONST support.)

Easiest solution here is either DIM SHARED, or to create a function for CRLF$.