QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Pete on February 01, 2020, 02:03:54 pm

Title: Is this possible in QB using TYPE.
Post by: Pete on February 01, 2020, 02:03:54 pm
Well, every couple of decades I like to try new things. The most recent has been to pass variables to subs almost exclusively of using global variables. Bill would be so proud! Anyway, that's great if I'm making small aps that do not reply on a lot of variables passed to a lot of subs. Right now, I'm in the middle of making a large app, with lots of subs, and I can see it is quickly going to be a PITA to have 20+ variables listed in a sub call! Now I know one way around this would be to use an array, where x$(1) = file_name$, x$(2) = "directory_location$, etc. but is ther anyway this could be done with TYPE, instead? Something like...

Code: QB64: [Select]
  1. TYPE mytype
  2.     a AS STRING
  3.     b AS STRING
  4.     c AS INTEGER
  5.  
  6. mytype.a$ = "Pete"
  7. mytype.b$ = "Steve"
  8. mytype.c = 5
  9. mytype = 123
  10.  
  11. CALL test(mytype)
  12.  
  13. SUB test (mytype)
  14.     PRINT mytype.a$, mytype.b$, mytype.c, mytype

I've tried (mytype AS TYPE) - rejected and (mytype AS mytype), also rejected.

Of course I could pass them as (mytype.a$, mytype.b$, mytype.c) but that doesn't save on the number of variables written to the sub call one bit... or byte.

Anyway, I'm either not using TYPE correctly for this, or it just isn't a "feature" of the language. I could go to the WIKI, but it's just not nearly as much as getting your wonderful replies like: "Look you stupid son of a %^&*#, it's done this way!!! Oh I forgot, Clippy doesn't show up around here anymore. Oh well.

Pete
Title: Re: Is this possible in QB using TYPE.
Post by: SMcNeill on February 01, 2020, 02:40:14 pm
DIM variable AS mytype

SUB test (subvariable AS mytype)

^Don’t forget to DIM your variables as your type.
Title: Re: Is this possible in QB using TYPE.
Post by: bplus on February 01, 2020, 02:42:47 pm
fixed
Code: QB64: [Select]
  1. TYPE mytype
  2.     a AS STRING
  3.     b AS STRING
  4.     c AS INTEGER
  5.  
  6. DIM this AS mytype
  7. this.a = "Pete"
  8. this.b = "Steve"
  9. this.c = 5
  10. 'mytype = 123  'no
  11.  
  12. test this
  13.  
  14. SUB test (x AS mytype)
  15.     PRINT x.a, x.b, x.c
  16.  

PS remember can use string for TYPE but can't file it, need fixed string for that.
Title: Re: Is this possible in QB using TYPE.
Post by: Pete on February 01, 2020, 03:05:39 pm
Way cool!!! That will cut down on all those nasty sub passes I didn't look forward to making.

Thanks guys... I didn't even get to say. "Sorry, you're not mytype!"

Now I'm left wondering what I'll be trying to do differently 20 years from now. Oh yeah, I know, a lid opening app... with hopes that battery life has improved immensely in the future. Hey, can caskets come equipped with LED lighting?

Pete