Author Topic: This UDT thing really bugs me...  (Read 5055 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
This UDT thing really bugs me...
« on: January 06, 2021, 04:58:12 pm »
You can use STATIC with UDTs, but unless I'm missing something, you can't pass a UDT as a single parameter. Instead, you have to do a DIM SHARED of the TYPE, or  pass each variable assigned to the UDT like page.x, page. y, page. z, etc. I don't see why you shouldn't be able to pass the type as a parameter. What are we, a Third World Language?

Pete

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

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: This UDT thing really bugs me...
« Reply #1 on: January 06, 2021, 04:59:37 pm »
Please excuse my ignorance... 'UDT'?
Logic is the beginning of wisdom.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: This UDT thing really bugs me...
« Reply #2 on: January 06, 2021, 05:09:46 pm »
what?

Code: QB64: [Select]
  1.  
  2. TYPE thing
  3.     whatever AS INTEGER
  4.     whateverelse AS DOUBLE
  5.  
  6. DIM dog AS thing
  7.  
  8. dog.whatever = 5
  9. dog.whateverelse = 0.33
  10.  
  11. PRINT dosomething(dog)
  12.  
  13.  
  14. FUNCTION dosomething (i AS thing)
  15.     DIM a AS INTEGER
  16.     DIM b AS DOUBLE
  17.     a = i.whatever
  18.     b = i.whateverelse
  19.     dosomething = a + b
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: This UDT thing really bugs me...
« Reply #3 on: January 06, 2021, 05:16:26 pm »
UDT stands for User Defined Type

Here is my what????
Code: QB64: [Select]
  1. TYPE UDT
  2.     x AS LONG
  3.     s AS STRING
  4. REDIM array(1 TO 20) AS UDT
  5. FOR i = 1 TO 20
  6.     array(i).x = i
  7.     array(i).s = CHR$(64 + i)
  8. FOR i = 2 TO 20 STEP 2
  9.     showArray array(), i
  10.  
  11.  
  12. SUB showArray (a() AS UDT, i)
  13.     PRINT a(i).x, a(i).s
  14.  

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: This UDT thing really bugs me...
« Reply #4 on: January 06, 2021, 05:20:12 pm »
Thanks bplus.
Logic is the beginning of wisdom.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: This UDT thing really bugs me...
« Reply #5 on: January 06, 2021, 06:16:49 pm »
UDT stands for User Defined Type

Here is my what????
Code: QB64: [Select]
  1. TYPE UDT
  2.     x AS LONG
  3.     s AS STRING
  4. REDIM array(1 TO 20) AS UDT
  5. FOR i = 1 TO 20
  6.     array(i).x = i
  7.     array(i).s = CHR$(64 + i)
  8. FOR i = 2 TO 20 STEP 2
  9.     showArray array(), i
  10.  
  11.  
  12. SUB showArray (a() AS UDT, i)
  13.     PRINT a(i).x, a(i).s
  14.  

So passed as "a" it works...

Code: QB64: [Select]
  1. TYPE UDT
  2.     x AS LONG
  3.     s AS STRING
  4. DIM array AS UDT
  5.  
  6. array.x = 10
  7. array.s = CHR$(65)
  8.  
  9. CALL showArray(array, i)
  10.  
  11. SUB showArray (a AS UDT, i)
  12.     PRINT a.x, a.s
  13.  

and this works...

Code: QB64: [Select]
  1. TYPE UDT
  2.     x AS LONG
  3.     s AS STRING
  4. DIM array AS UDT
  5.  
  6. array.x = 10
  7. array.s = CHR$(65i)
  8.  
  9. CALL showArray(array, i)
  10.  
  11. SUB showArray (array, i)
  12.     PRINT array.x, array.s
  13.  

but if you don't use array as UDT in the SUB, it fails...

Code: QB64: [Select]
  1. TYPE UDT
  2.     x AS LONG
  3.     s AS STRING
  4. DIM array AS UDT
  5.  
  6. array.x = 10
  7. array.s = CHR$(65)
  8.  
  9. CALL showArray(array, i)
  10.  
  11. SUB showArray (array, i)
  12.     PRINT array.x, array.s
  13.  

My guess is the reason is something like you have to not only pass the variable, but when SHARED is not used, you have to pass the type to the sub, along with the variable. If that's the reason, maybe it should be noted that you couldn't write array = 10 in the code, either. That stated, what other reason(s), if any, would prevent QB64 from allowing the last example to compile and work?

Pete
« Last Edit: January 06, 2021, 06:24:45 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: This UDT thing really bugs me...
« Reply #6 on: January 06, 2021, 06:19:29 pm »
what the hell is

Code: QB64: [Select]
  1. CHR$(65i)
You're not done when it works, you're done when it's right.

FellippeHeitor

  • Guest
Re: This UDT thing really bugs me...
« Reply #7 on: January 06, 2021, 06:20:02 pm »
If you don't write AS UDT, the array parameter is a SINGLE type variable.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: This UDT thing really bugs me...
« Reply #8 on: January 06, 2021, 06:21:12 pm »
Ah, came back to say what Fellippe just said ^.

Funny, cause all he did was copypaste my example but deleted too much :-)
You're not done when it works, you're done when it's right.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: This UDT thing really bugs me...
« Reply #9 on: January 06, 2021, 06:21:44 pm »
what the hell is

Code: QB64: [Select]
  1. CHR$(65i)

It's a TYPeO.

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

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: This UDT thing really bugs me...
« Reply #10 on: January 06, 2021, 06:27:30 pm »
If you don't write AS UDT, the array parameter is a SINGLE type variable.

No it isn't, because it was DIMmed as a TYPE. That's why the IDE won't accept it as a variable.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

FellippeHeitor

  • Guest
Re: This UDT thing really bugs me...
« Reply #11 on: January 06, 2021, 06:32:09 pm »
Code: QB64: [Select]
  1. TYPE UDT
  2.     x AS LONG
  3.     s AS STRING
  4. DIM array AS UDT
  5.  
  6. array.x = 10
  7. array.s = CHR$(65)
  8.  
  9. CALL showArray(array, i)
  10.  
  11. SUB showArray (array AS UDT, i)
  12.     PRINT array.x, array.s
  13.  
  14.  

FellippeHeitor

  • Guest
Re: This UDT thing really bugs me...
« Reply #12 on: January 06, 2021, 06:32:49 pm »
'array' in the main module is a separate entity from 'array' as a parameter to a sub.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: This UDT thing really bugs me...
« Reply #13 on: January 06, 2021, 06:47:31 pm »
And that was my reasoning, too, but since QB64 won't allow "array" to be used as a variable, once it is assigned as a UDT, maybe it could be made to be passed as...

Code: QB64: [Select]
  1. TYPE UDT
  2.     x AS LONG
  3.     s AS STRING
  4. DIM array AS UDT
  5.  
  6. array.x = 10
  7. array.s = CHR$(65)
  8.  
  9. CALL showArray(array, i)
  10.  
  11. SUB showArray (array, i)
  12.     PRINT array.x, array.s
  13.  

What "bugs" me about having to add the "AS UDT" in the sub is the extra size it adds to the line of code, making it more difficult to read when there are several additional parameters passed.

My guess is as much as I would like my abbreviated example, it would conflict with too many established constructs such as...

Code: QB64: [Select]
  1.  
  2. CALL showArray(a)
  3.  
  4. SUB showArray (a)
  5.     a = 1.5
  6.     PRINT a

vs...

Code: QB64: [Select]
  1.  
  2. CALL showArray(a)
  3.  
  4. SUB showArray (a AS INTEGER)
  5.     a = 1.5
  6.     PRINT a

...for examples.

Well, as much as I have't worked with UDT's in the past, I may just bite the bullet on this one, and consider turning more of my variables into UDT's. That way, I can pass more variables under fewer parameters, and shorten those SUB () lines.

Thanks for batting this around with me.

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

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: This UDT thing really bugs me...
« Reply #14 on: January 06, 2021, 06:55:38 pm »
My guess is as much as I would like my abbreviated example, it would conflict with too many established constructs such as...

Code: QB64: [Select]
  1.  
  2. CALL showArray(a)
  3.  
  4. SUB showArray (a)
  5.     a = 1.5
  6.     PRINT a

I find it amusing you refer to this as established usage, since this is QB64-only code. QB45 will complain about a type mismatch, because you're trying to pass an integer by-reference to a function expecting a single. QB64 is being "helpful" and implicitly adding a CINT() around your argument.

This may cause surprises if you expect the parameter to be an output parameter:
Code: QB64: [Select]
  1. p = 2
  2. q = 3
  3. PRINT p, q
  4. xchg p, q
  5. PRINT p, q
  6.  
  7. SUB xchg (a, b)
  8.     temp = a
  9.     a = b
  10.     b = temp
Doesn't swap the values because they're being passed by-value (and yes, I know we already have a SWAP command).