Why element size is not 2? - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Why element size is not 2? (/showthread.php?tid=1636) |
Why element size is not 2? - Petr - 04-23-2023 Code: (Select All) Type snd1 Is there any way to fix this, or does that mean I have to completely rewrite 90 percent of the program that relied on this? RE: Why element size is not 2? - Petr - 04-23-2023 Maybe it will be related to some medieval definition from qbasic.... (I'm assuming that you can't do for example DIM L as INTEGER and then DIM L as LONG, an error will occur - the variable is already used, so this will probably be the same case, but there is a lack of error catching in the IDE. (that's a guess). I will this ancient limitation bypass via MEM. RE: Why element size is not 2? - SMcNeill - 04-23-2023 Honestly, this shouldn't even be possible. You should get an Invalid Type error, but it's not catching it in this case. DIM s(0) AS INTEGER DIM s(0) AS _UNSIGNED _BYTE The above should error out for you with an Invalid Type or Duplicate Definition or similar error. Why it's not tossing that same style error for a UDT needs to be looked into. RE: Why element size is not 2? - bplus - 04-23-2023 Gotta say it looks like a bug! Further test reveals it does accept an integer argument as integer: Code: (Select All) Type snd1 If the first s(0) could be cleared from memory when s(0) was REDIM'd, Len() might get it's facts straight. Are there 2 s(0)'s such that Len() gets the first one set up in variable table? RE: Why element size is not 2? - bplus - 04-23-2023 Look what happens when try to REDIM s(0) as a normal variable: Code: (Select All) Type snd1 RE: Why element size is not 2? - Petr - 04-23-2023 @BPlus This is the same as what I write about above, 55 will remain there because it is in the range for the _Unsigned _Byte type. Once the field is defined as an unsigned byte, it simply stays that way forever. LEN for _Unsigned _Byte is 1 (lenght is 1 byte) To confirm, insert the number 355 instead of 55, the Unsigned Byte data type will overflow and PRINT will print 355 - 255 = 99 (because even zero is taken into account) and LEN return variable LENGHT (byte - size), not variable value, so this can not be deleted. RE: Why element size is not 2? - bplus - 04-23-2023 (04-23-2023, 03:31 PM)Petr Wrote: @BPlus Oh right! sorry don't use Byte that much, thanks for reminder. Still isn't interesting you can REDIM an UDT of same name but not a regular variable. Another example: Code: (Select All) ReDim x As Integer You shouldn't be allowed to REDIM the 2nd s(0) as a different type than original, if you wish consistency to rule in your PL, not a great rule for Basic though ;-)) RE: Why element size is not 2? - mnrvovrfc - 04-23-2023 (04-23-2023, 11:57 AM)Petr Wrote: Are you trying to do an "union" type like in C? :O Why are you trying to redimension the same array only to change the types of the same fields? Maybe you already know but you have to use _MEM on a variable which is more than one byte in size, and you need to extract it byte by byte or something else. OK I saw the second post here... Be glad that "Integer" will always be 2 bytes in anything directly compatible with QuickBASIC and QBasic. Because in Freebasic they decided to throw it out of whack. For that language many programs for QBasic would have to be rewritten to change from "As Integer" to "As Short" if the programmer really meant 2-byte integer. RE: Why element size is not 2? - bplus - 04-23-2023 @mnrvovrfc I think he wants to use same code on 2 different Types of UDT. Lazy or smart? if you can get away with it, smart! otherwise, stupid and have to rewrite everything involving those UDTs. RE: Why element size is not 2? - SMcNeill - 04-23-2023 I've did sometime similar in the past with CustomType Self-Refrencing libraries: Code: (Select All) DECLARE CUSTOMTYPE LIBRARY 'Use Customtype for self-referencing a sub written inside your program |