Author Topic: setting string constant to a chr$ returning type mismatch?  (Read 3207 times)

0 Members and 1 Guest are viewing this topic.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
setting string constant to a chr$ returning type mismatch?
« 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...

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: setting string constant to a chr$ returning type mismatch?
« Reply #1 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.

Marked as best answer by madscijr on December 22, 2020, 01:44:25 pm

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: setting string constant to a chr$ returning type mismatch?
« Reply #2 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)
« Last Edit: December 22, 2020, 01:47:03 pm by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: setting string constant to a chr$ returning type mismatch?
« Reply #3 on: December 22, 2020, 02:07:36 pm »
Yeah that's another way!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: setting string constant to a chr$ returning type mismatch?
« Reply #4 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.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: setting string constant to a chr$ returning type mismatch?
« Reply #5 on: December 22, 2020, 07:24:13 pm »
I think coders overuse CONST. I seldom find a use for it.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: setting string constant to a chr$ returning type mismatch?
« Reply #6 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.
Shuwatch!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: setting string constant to a chr$ returning type mismatch?
« Reply #7 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!
Programming isn't difficult, only it's  consuming time and coffee

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: setting string constant to a chr$ returning type mismatch?
« Reply #8 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

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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: setting string constant to a chr$ returning type mismatch?
« Reply #9 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

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.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: setting string constant to a chr$ returning type mismatch?
« Reply #10 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$.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!