Author Topic: Error with Constant comparison in SELECT CASE?  (Read 1622 times)

0 Members and 1 Guest are viewing this topic.

Offline daro

  • Newbie
  • Posts: 1
    • View Profile
Error with Constant comparison in SELECT CASE?
« on: May 17, 2021, 08:51:48 am »
I'm finding this strange error when using a constant inside of a select case, any ideas why?
I've tried many different ways but it seems to be the use of a CONST inside of the SELECT CASE giving the error.

Expected string expression on line 12
Caused by (or after) CASE "A", 1 , "a",1 , KEY_ARROW_LEFT


Code: QB64: [Select]
  1. Const KEY_ARROW_LEFT = Chr$(0) + Chr$(72)
  2. Const KEY_ARROW_UP = Chr$(0) + Chr$(72)
  3. Const KEY_ARROW_RIGHT = Chr$(0) + Chr$(77)
  4. Const KEY_ARROW_DOWN = Chr$(0) + Chr$(80)
  5. Const KEY_PAGE_UP = Chr$(0) + Chr$(73)
  6. Const KEY_PAGE_DOWN = Chr$(0) + Chr$(81)
  7.  
  8.  
  9.     keyp$ = InKey$
  10.     Select Case keyp$
  11.         Case "A", "a", KEY_ARROW_LEFT
  12.             Print " Left ";
  13.         Case "W", "w", KEY_ARROW_UP
  14.             Print " Up   ";
  15.         Case "D", "d" , KEY_ARROW_RIGHT
  16.             Print " Right";
  17.         Case "S", "s", KEY_ARROW_DOWN
  18.             Print " Down ";
  19.         Case "Z", "z", KEY_PAGE_DOWN
  20.             Print " P.Up ";
  21.         Case "X","x", KEY_PAGE_UP
  22.             Print "P.Down ";
  23.         Case KEY_ESCAPE
  24.             Exit Do
  25.     End Select
  26.  
« Last Edit: May 17, 2021, 08:57:20 am by daro »

Marked as best answer by daro on May 17, 2021, 07:24:55 pm

FellippeHeitor

  • Guest
Re: Error with Constant comparison in SELECT CASE?
« Reply #1 on: May 17, 2021, 09:56:15 am »
@daro Looks indeed like a bug. We'll have a look.

In the meantime, make those DIM SHARED AS STRING instead of CONSTs, for the same expected effect:

Code: QB64: [Select]
  1. DIM SHARED AS STRING KEY_ARROW_LEFT, KEY_ARROW_UP, KEY_ARROW_RIGHT, KEY_ARROW_DOWN, KEY_PAGE_UP, KEY_PAGE_DOWN
  2. KEY_ARROW_LEFT = Chr$(0) + Chr$(72)
  3. KEY_ARROW_UP = Chr$(0) + Chr$(72)
  4. KEY_ARROW_RIGHT = Chr$(0) + Chr$(77)
  5. KEY_ARROW_DOWN = Chr$(0) + Chr$(80)
  6. KEY_PAGE_UP = Chr$(0) + Chr$(73)
  7. KEY_PAGE_DOWN = Chr$(0) + Chr$(81)
  8.  

Welcome aboard.
« Last Edit: May 17, 2021, 09:57:58 am by FellippeHeitor »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Error with Constant comparison in SELECT CASE?
« Reply #2 on: May 17, 2021, 02:31:06 pm »
I would recommend using _KEYHIT or _KEYDOWN over INKEY$ and changing that SELECT CASE into IF THENs.


@FellippeHeitor
Doesn't CONST have issues when using variables as assignments?
Wasn't there something not that long ago about using a variable to assign constant?
something like:
A%=15-3
CONST VALUE = A%

I can't quite remember now. not having any luck with the search either, but I could swear we've had something like this before.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Error with Constant comparison in SELECT CASE?
« Reply #3 on: May 17, 2021, 02:36:39 pm »
I would recommend using _KEYHIT or _KEYDOWN over INKEY$ and changing that SELECT CASE into IF THENs.


@FellippeHeitor
Doesn't CONST have issues when using variables as assignments?
Wasn't there something not that long ago about using a variable to assign constant?
something like:
A%=15-3
CONST VALUE = A%

I can't quite remember now. not having any luck with the search either, but I could swear we've had something like this before.

We've had trouble using Functions and doing math but I think it's been fixed. Maybe not all string stuff is, I think we are using Steve Math Evaluator?

FellippeHeitor

  • Guest
Re: Error with Constant comparison in SELECT CASE?
« Reply #4 on: May 21, 2021, 08:09:26 pm »
Turns out the CONST evaluator was never meant to be used with string functions, like Chr$, Space$, etc. - but Chr$ had been left there as valid - just a slip. The latest dev build generated today fixes that by not allowing string functions in CONSTs. This is a list of the functions that can be used in CONSTs. The wiki page for CONST is getting updated too.

Code: QB64: [Select]
« Last Edit: May 21, 2021, 08:25:48 pm by FellippeHeitor »

FellippeHeitor

  • Guest
Re: Error with Constant comparison in SELECT CASE?
« Reply #5 on: May 21, 2021, 08:30:00 pm »
I would recommend using _KEYHIT or _KEYDOWN over INKEY$ and changing that SELECT CASE into IF THENs.


@FellippeHeitor
Doesn't CONST have issues when using variables as assignments?
Wasn't there something not that long ago about using a variable to assign constant?
something like:
A%=15-3
CONST VALUE = A%

I can't quite remember now. not having any luck with the search either, but I could swear we've had something like this before.

It was never really allowed with variables, but we did have a problem recently (fixed in 1.5) that would cause issues when referencing existing CONSTs in a new CONST assignment.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Error with Constant comparison in SELECT CASE?
« Reply #6 on: May 22, 2021, 10:46:42 am »
edit: OP wanted strings not numbers
« Last Edit: May 22, 2021, 11:05:42 am by bplus »