Use an array in a user-defined TYPE in QBasic












1















I'm trying to learn QBasic to program on an Amstrad Alt-286. In one of my program, I use several user-defined types, sometimes TYPE arrays. In some of them, I want to declare an array like this :



TYPE TestType
dataArray AS STRING * 4 'Since "dataArray AS _BYTE * 4" doesn't work (wrong syntax compiler says).
END TYPE


I then declare my type like this :



DIM customType(2) AS TestType


And as soon as I want to write in my type's dataArray like this :



customType(1).dataArray(2) = 3


The compiler tells me it is an invalid syntax.



Then, how to store an array in a defined TYPE?
And how to use it?










share|improve this question




















  • 1





    You can't use arrays or variable-length STRING members inside a TYPE as far as I know. If you desire array-like functionality, QB64 offers the _MEM type, and you can define dataArray AS _MEM. You'll most likely be interested in the _MEMNEW and _MEMFREE functions to allocate and deallocate the memory block (see the "See Also" section of the docs). You may want to explore the values of the fields of the _MEM type with variables of differing types using the _MEM function since this aspect is documented somewhat poorly at the moment.

    – Chrono Kitsune
    Nov 24 '18 at 4:13











  • @ChronoKitsune Since my program should run on a. Amstrad Alt-286, do you know if the _MEM type and its associated fuctions have an equivalent in qb 4.5 or earlier versions?

    – Maxime Beasse
    Nov 24 '18 at 11:43






  • 1





    Unfortunately, no. I mentioned _MEM since the QB64 tag was added, and AFAIK, you can't target the Amstrad Alt-286 using QB64. QB64 is intended to get old QB 4.5 programs running on modern 32-bit and 64-bit platforms such as Windows, macOS/OS X, and Linux. I'm afraid I can't help with your target since I don't know enough about it.

    – Chrono Kitsune
    Nov 24 '18 at 19:24
















1















I'm trying to learn QBasic to program on an Amstrad Alt-286. In one of my program, I use several user-defined types, sometimes TYPE arrays. In some of them, I want to declare an array like this :



TYPE TestType
dataArray AS STRING * 4 'Since "dataArray AS _BYTE * 4" doesn't work (wrong syntax compiler says).
END TYPE


I then declare my type like this :



DIM customType(2) AS TestType


And as soon as I want to write in my type's dataArray like this :



customType(1).dataArray(2) = 3


The compiler tells me it is an invalid syntax.



Then, how to store an array in a defined TYPE?
And how to use it?










share|improve this question




















  • 1





    You can't use arrays or variable-length STRING members inside a TYPE as far as I know. If you desire array-like functionality, QB64 offers the _MEM type, and you can define dataArray AS _MEM. You'll most likely be interested in the _MEMNEW and _MEMFREE functions to allocate and deallocate the memory block (see the "See Also" section of the docs). You may want to explore the values of the fields of the _MEM type with variables of differing types using the _MEM function since this aspect is documented somewhat poorly at the moment.

    – Chrono Kitsune
    Nov 24 '18 at 4:13











  • @ChronoKitsune Since my program should run on a. Amstrad Alt-286, do you know if the _MEM type and its associated fuctions have an equivalent in qb 4.5 or earlier versions?

    – Maxime Beasse
    Nov 24 '18 at 11:43






  • 1





    Unfortunately, no. I mentioned _MEM since the QB64 tag was added, and AFAIK, you can't target the Amstrad Alt-286 using QB64. QB64 is intended to get old QB 4.5 programs running on modern 32-bit and 64-bit platforms such as Windows, macOS/OS X, and Linux. I'm afraid I can't help with your target since I don't know enough about it.

    – Chrono Kitsune
    Nov 24 '18 at 19:24














1












1








1


1






I'm trying to learn QBasic to program on an Amstrad Alt-286. In one of my program, I use several user-defined types, sometimes TYPE arrays. In some of them, I want to declare an array like this :



TYPE TestType
dataArray AS STRING * 4 'Since "dataArray AS _BYTE * 4" doesn't work (wrong syntax compiler says).
END TYPE


I then declare my type like this :



DIM customType(2) AS TestType


And as soon as I want to write in my type's dataArray like this :



customType(1).dataArray(2) = 3


The compiler tells me it is an invalid syntax.



Then, how to store an array in a defined TYPE?
And how to use it?










share|improve this question
















I'm trying to learn QBasic to program on an Amstrad Alt-286. In one of my program, I use several user-defined types, sometimes TYPE arrays. In some of them, I want to declare an array like this :



TYPE TestType
dataArray AS STRING * 4 'Since "dataArray AS _BYTE * 4" doesn't work (wrong syntax compiler says).
END TYPE


I then declare my type like this :



DIM customType(2) AS TestType


And as soon as I want to write in my type's dataArray like this :



customType(1).dataArray(2) = 3


The compiler tells me it is an invalid syntax.



Then, how to store an array in a defined TYPE?
And how to use it?







arrays qbasic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 0:04







Maxime Beasse

















asked Nov 21 '18 at 10:27









Maxime BeasseMaxime Beasse

209




209








  • 1





    You can't use arrays or variable-length STRING members inside a TYPE as far as I know. If you desire array-like functionality, QB64 offers the _MEM type, and you can define dataArray AS _MEM. You'll most likely be interested in the _MEMNEW and _MEMFREE functions to allocate and deallocate the memory block (see the "See Also" section of the docs). You may want to explore the values of the fields of the _MEM type with variables of differing types using the _MEM function since this aspect is documented somewhat poorly at the moment.

    – Chrono Kitsune
    Nov 24 '18 at 4:13











  • @ChronoKitsune Since my program should run on a. Amstrad Alt-286, do you know if the _MEM type and its associated fuctions have an equivalent in qb 4.5 or earlier versions?

    – Maxime Beasse
    Nov 24 '18 at 11:43






  • 1





    Unfortunately, no. I mentioned _MEM since the QB64 tag was added, and AFAIK, you can't target the Amstrad Alt-286 using QB64. QB64 is intended to get old QB 4.5 programs running on modern 32-bit and 64-bit platforms such as Windows, macOS/OS X, and Linux. I'm afraid I can't help with your target since I don't know enough about it.

    – Chrono Kitsune
    Nov 24 '18 at 19:24














  • 1





    You can't use arrays or variable-length STRING members inside a TYPE as far as I know. If you desire array-like functionality, QB64 offers the _MEM type, and you can define dataArray AS _MEM. You'll most likely be interested in the _MEMNEW and _MEMFREE functions to allocate and deallocate the memory block (see the "See Also" section of the docs). You may want to explore the values of the fields of the _MEM type with variables of differing types using the _MEM function since this aspect is documented somewhat poorly at the moment.

    – Chrono Kitsune
    Nov 24 '18 at 4:13











  • @ChronoKitsune Since my program should run on a. Amstrad Alt-286, do you know if the _MEM type and its associated fuctions have an equivalent in qb 4.5 or earlier versions?

    – Maxime Beasse
    Nov 24 '18 at 11:43






  • 1





    Unfortunately, no. I mentioned _MEM since the QB64 tag was added, and AFAIK, you can't target the Amstrad Alt-286 using QB64. QB64 is intended to get old QB 4.5 programs running on modern 32-bit and 64-bit platforms such as Windows, macOS/OS X, and Linux. I'm afraid I can't help with your target since I don't know enough about it.

    – Chrono Kitsune
    Nov 24 '18 at 19:24








1




1





You can't use arrays or variable-length STRING members inside a TYPE as far as I know. If you desire array-like functionality, QB64 offers the _MEM type, and you can define dataArray AS _MEM. You'll most likely be interested in the _MEMNEW and _MEMFREE functions to allocate and deallocate the memory block (see the "See Also" section of the docs). You may want to explore the values of the fields of the _MEM type with variables of differing types using the _MEM function since this aspect is documented somewhat poorly at the moment.

– Chrono Kitsune
Nov 24 '18 at 4:13





You can't use arrays or variable-length STRING members inside a TYPE as far as I know. If you desire array-like functionality, QB64 offers the _MEM type, and you can define dataArray AS _MEM. You'll most likely be interested in the _MEMNEW and _MEMFREE functions to allocate and deallocate the memory block (see the "See Also" section of the docs). You may want to explore the values of the fields of the _MEM type with variables of differing types using the _MEM function since this aspect is documented somewhat poorly at the moment.

– Chrono Kitsune
Nov 24 '18 at 4:13













@ChronoKitsune Since my program should run on a. Amstrad Alt-286, do you know if the _MEM type and its associated fuctions have an equivalent in qb 4.5 or earlier versions?

– Maxime Beasse
Nov 24 '18 at 11:43





@ChronoKitsune Since my program should run on a. Amstrad Alt-286, do you know if the _MEM type and its associated fuctions have an equivalent in qb 4.5 or earlier versions?

– Maxime Beasse
Nov 24 '18 at 11:43




1




1





Unfortunately, no. I mentioned _MEM since the QB64 tag was added, and AFAIK, you can't target the Amstrad Alt-286 using QB64. QB64 is intended to get old QB 4.5 programs running on modern 32-bit and 64-bit platforms such as Windows, macOS/OS X, and Linux. I'm afraid I can't help with your target since I don't know enough about it.

– Chrono Kitsune
Nov 24 '18 at 19:24





Unfortunately, no. I mentioned _MEM since the QB64 tag was added, and AFAIK, you can't target the Amstrad Alt-286 using QB64. QB64 is intended to get old QB 4.5 programs running on modern 32-bit and 64-bit platforms such as Windows, macOS/OS X, and Linux. I'm afraid I can't help with your target since I don't know enough about it.

– Chrono Kitsune
Nov 24 '18 at 19:24












2 Answers
2






active

oldest

votes


















2














There are two issues here. In QB64 you simply can't put arrays inside of user defined types. According to the QB64 Wiki's article on TYPE definitions:




TYPE definitions cannot contain Array variables! Arrays can be DIMensioned as a TYPE definition.




Besides that, your dataArray (declared dataArray AS STRING * 4) does not declare an array at all, but rather, declares a 4 character string. That's why you get a syntax error when you try to access elements of dataArray using array syntax. You can declare an array consisting of a custom type, like so:



TYPE TestType
dataElement AS _BYTE
END TYPE

DIM CustomType(4) AS TestType

CustomType(1).dataElement = 3


This declares a 4 element array of TYPE TestType, each element containing a variable of TYPE _BYTE. That's about as close as you can get to what you're trying to do. Good luck!






share|improve this answer

































    0














    The code you want is something like this:



    Although you CANNOT do this in QB1.1, QB4.5, or QB64, you CAN do this in supersets of the BASIC dialect known as QB7.1(BC7/PDS), and VBDOS(v1.00):



    TYPE testtype
    dataArray(4) AS INTEGER
    END TYPE
    DIM customtype(10) AS testtype
    customtype(1).dataArray(2) = 3


    Otherwise you could compress the variables as such:



    TYPE testtype
    dataArray AS STRING * 8
    END TYPE
    DIM customtype(10) AS testtype
    A = 10: B = 12: C = 14: D = 16
    ' compress variables into structure
    element1$ = MKI$(A) + MKI$(B) + MKI$(C) + MKI$(D)
    customtype(1).dataArray = element1$ ' store
    ' extract variables from structure
    element2$ = customtype(1).dataArray ' get
    E = CVI(MID$(element2$, 1, 2))
    F = CVI(MID$(element2$, 3, 2))
    G = CVI(MID$(element2$, 5, 2))
    H = CVI(MID$(element2$, 7, 2))
    PRINT E, F, G, H





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410020%2fuse-an-array-in-a-user-defined-type-in-qbasic%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      There are two issues here. In QB64 you simply can't put arrays inside of user defined types. According to the QB64 Wiki's article on TYPE definitions:




      TYPE definitions cannot contain Array variables! Arrays can be DIMensioned as a TYPE definition.




      Besides that, your dataArray (declared dataArray AS STRING * 4) does not declare an array at all, but rather, declares a 4 character string. That's why you get a syntax error when you try to access elements of dataArray using array syntax. You can declare an array consisting of a custom type, like so:



      TYPE TestType
      dataElement AS _BYTE
      END TYPE

      DIM CustomType(4) AS TestType

      CustomType(1).dataElement = 3


      This declares a 4 element array of TYPE TestType, each element containing a variable of TYPE _BYTE. That's about as close as you can get to what you're trying to do. Good luck!






      share|improve this answer






























        2














        There are two issues here. In QB64 you simply can't put arrays inside of user defined types. According to the QB64 Wiki's article on TYPE definitions:




        TYPE definitions cannot contain Array variables! Arrays can be DIMensioned as a TYPE definition.




        Besides that, your dataArray (declared dataArray AS STRING * 4) does not declare an array at all, but rather, declares a 4 character string. That's why you get a syntax error when you try to access elements of dataArray using array syntax. You can declare an array consisting of a custom type, like so:



        TYPE TestType
        dataElement AS _BYTE
        END TYPE

        DIM CustomType(4) AS TestType

        CustomType(1).dataElement = 3


        This declares a 4 element array of TYPE TestType, each element containing a variable of TYPE _BYTE. That's about as close as you can get to what you're trying to do. Good luck!






        share|improve this answer




























          2












          2








          2







          There are two issues here. In QB64 you simply can't put arrays inside of user defined types. According to the QB64 Wiki's article on TYPE definitions:




          TYPE definitions cannot contain Array variables! Arrays can be DIMensioned as a TYPE definition.




          Besides that, your dataArray (declared dataArray AS STRING * 4) does not declare an array at all, but rather, declares a 4 character string. That's why you get a syntax error when you try to access elements of dataArray using array syntax. You can declare an array consisting of a custom type, like so:



          TYPE TestType
          dataElement AS _BYTE
          END TYPE

          DIM CustomType(4) AS TestType

          CustomType(1).dataElement = 3


          This declares a 4 element array of TYPE TestType, each element containing a variable of TYPE _BYTE. That's about as close as you can get to what you're trying to do. Good luck!






          share|improve this answer















          There are two issues here. In QB64 you simply can't put arrays inside of user defined types. According to the QB64 Wiki's article on TYPE definitions:




          TYPE definitions cannot contain Array variables! Arrays can be DIMensioned as a TYPE definition.




          Besides that, your dataArray (declared dataArray AS STRING * 4) does not declare an array at all, but rather, declares a 4 character string. That's why you get a syntax error when you try to access elements of dataArray using array syntax. You can declare an array consisting of a custom type, like so:



          TYPE TestType
          dataElement AS _BYTE
          END TYPE

          DIM CustomType(4) AS TestType

          CustomType(1).dataElement = 3


          This declares a 4 element array of TYPE TestType, each element containing a variable of TYPE _BYTE. That's about as close as you can get to what you're trying to do. Good luck!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 0:07

























          answered Nov 22 '18 at 17:59









          seisvelasseisvelas

          2,03911229




          2,03911229

























              0














              The code you want is something like this:



              Although you CANNOT do this in QB1.1, QB4.5, or QB64, you CAN do this in supersets of the BASIC dialect known as QB7.1(BC7/PDS), and VBDOS(v1.00):



              TYPE testtype
              dataArray(4) AS INTEGER
              END TYPE
              DIM customtype(10) AS testtype
              customtype(1).dataArray(2) = 3


              Otherwise you could compress the variables as such:



              TYPE testtype
              dataArray AS STRING * 8
              END TYPE
              DIM customtype(10) AS testtype
              A = 10: B = 12: C = 14: D = 16
              ' compress variables into structure
              element1$ = MKI$(A) + MKI$(B) + MKI$(C) + MKI$(D)
              customtype(1).dataArray = element1$ ' store
              ' extract variables from structure
              element2$ = customtype(1).dataArray ' get
              E = CVI(MID$(element2$, 1, 2))
              F = CVI(MID$(element2$, 3, 2))
              G = CVI(MID$(element2$, 5, 2))
              H = CVI(MID$(element2$, 7, 2))
              PRINT E, F, G, H





              share|improve this answer






























                0














                The code you want is something like this:



                Although you CANNOT do this in QB1.1, QB4.5, or QB64, you CAN do this in supersets of the BASIC dialect known as QB7.1(BC7/PDS), and VBDOS(v1.00):



                TYPE testtype
                dataArray(4) AS INTEGER
                END TYPE
                DIM customtype(10) AS testtype
                customtype(1).dataArray(2) = 3


                Otherwise you could compress the variables as such:



                TYPE testtype
                dataArray AS STRING * 8
                END TYPE
                DIM customtype(10) AS testtype
                A = 10: B = 12: C = 14: D = 16
                ' compress variables into structure
                element1$ = MKI$(A) + MKI$(B) + MKI$(C) + MKI$(D)
                customtype(1).dataArray = element1$ ' store
                ' extract variables from structure
                element2$ = customtype(1).dataArray ' get
                E = CVI(MID$(element2$, 1, 2))
                F = CVI(MID$(element2$, 3, 2))
                G = CVI(MID$(element2$, 5, 2))
                H = CVI(MID$(element2$, 7, 2))
                PRINT E, F, G, H





                share|improve this answer




























                  0












                  0








                  0







                  The code you want is something like this:



                  Although you CANNOT do this in QB1.1, QB4.5, or QB64, you CAN do this in supersets of the BASIC dialect known as QB7.1(BC7/PDS), and VBDOS(v1.00):



                  TYPE testtype
                  dataArray(4) AS INTEGER
                  END TYPE
                  DIM customtype(10) AS testtype
                  customtype(1).dataArray(2) = 3


                  Otherwise you could compress the variables as such:



                  TYPE testtype
                  dataArray AS STRING * 8
                  END TYPE
                  DIM customtype(10) AS testtype
                  A = 10: B = 12: C = 14: D = 16
                  ' compress variables into structure
                  element1$ = MKI$(A) + MKI$(B) + MKI$(C) + MKI$(D)
                  customtype(1).dataArray = element1$ ' store
                  ' extract variables from structure
                  element2$ = customtype(1).dataArray ' get
                  E = CVI(MID$(element2$, 1, 2))
                  F = CVI(MID$(element2$, 3, 2))
                  G = CVI(MID$(element2$, 5, 2))
                  H = CVI(MID$(element2$, 7, 2))
                  PRINT E, F, G, H





                  share|improve this answer















                  The code you want is something like this:



                  Although you CANNOT do this in QB1.1, QB4.5, or QB64, you CAN do this in supersets of the BASIC dialect known as QB7.1(BC7/PDS), and VBDOS(v1.00):



                  TYPE testtype
                  dataArray(4) AS INTEGER
                  END TYPE
                  DIM customtype(10) AS testtype
                  customtype(1).dataArray(2) = 3


                  Otherwise you could compress the variables as such:



                  TYPE testtype
                  dataArray AS STRING * 8
                  END TYPE
                  DIM customtype(10) AS testtype
                  A = 10: B = 12: C = 14: D = 16
                  ' compress variables into structure
                  element1$ = MKI$(A) + MKI$(B) + MKI$(C) + MKI$(D)
                  customtype(1).dataArray = element1$ ' store
                  ' extract variables from structure
                  element2$ = customtype(1).dataArray ' get
                  E = CVI(MID$(element2$, 1, 2))
                  F = CVI(MID$(element2$, 3, 2))
                  G = CVI(MID$(element2$, 5, 2))
                  H = CVI(MID$(element2$, 7, 2))
                  PRINT E, F, G, H






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 27 at 19:49

























                  answered Jan 27 at 19:29









                  eoredsoneoredson

                  68111027




                  68111027






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410020%2fuse-an-array-in-a-user-defined-type-in-qbasic%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()