Announcement

Collapse
No announcement yet.

User Defined Types (Udts)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • User Defined Types (Udts)

    Please add your comments and suggestions as a Reply to this thread.





    PB/WIN - User-Defined Types (UDTs)
    Arrays are useful when you need to treat a set of similar variables as a unit. For instance, ten test scores or ten student names. But what if you need to store several unrelated data types and be able to treat them as a unit? That is where User-Defined Types come in. When you define a User-Defined Type (UDT), you are actually defining a template for a new variable Type.

    Once created, you can define as many variables of your new Type as you please. Moreover, since User-Defined Types can be associated with a random file's buffer, this provides you with a whole new way to access your random files.

    PowerBASIC's User-Defined Type is similar to a C struct or Pascal record. The elements of a User-Defined Type may include any of PowerBASIC's data types, with the exception of dynamic (variable-length) strings, field strings, and arrays of dynamic strings.

    To get an idea of the power of the User-Defined Type, imagine you are a teacher who needs a program to keep track of student grades. Since your school is on a very tight budget (and what schools aren't these days?), you decide to write the program yourself in PowerBASIC. For each student in the class you need to track the following information:
    • The student's name
    • A student number
    • A mailing address
    • The name and phone of the person to contact in case of an emergency
    • The relationship of the contact person to the student

    Currently these records are being kept in a small file box. The information about each student is contained on a single file card. How do you transfer this information to the computer? Simple. Define a Student Record Type that will contain all the information about a single student.

    The variables you create as User-Defined Types are often called records or record variables, since each variable of that Type contains one record, or one set of related information. The individual elements are referred to as fields or members. In the example above, each set of student information is a record, and each piece of information within that record (the last name for example) is a field.
    See Also
    Last edited by Gary Beene; 28 Oct 2014, 10:48 PM.

  • #2
    I'm a big user of UDTs, but I had to conduct experiments to determine the behavior expected when dealing with them, especially when writing data into their fields.

    For instance, if I have a UDT with a 48-byte string field named RecDat.CName and I write data to it directly from a textbox using:
    Code:
    CONTROL GET TEXT hDlg, 100 TO RecDat.CName
    How will I know the text is LSet in the RecDat.CName field?
    If the textbox field length is less than the RecDat.CName field, what will the field be padded with?
    If the text length is greater than the RecDat.CName field length, will it be truncated, or will the following field in the RecDat array be corrupted.

    I was unable to find any examples in the PB program samples and no information in the online help.

    The online help for UDTs needs to provide additional information that addresses these issues.
    It's impossible to make anything foolproof because fools are so ingenious.

    Comment


    • #3
      for variable-length strings a UDT usually stores the length of the string as an additional accompanying field, ie
      Code:
      TYPE MyStr
        ptrStr AS STRING PTR
        dwStrLen AS DWORD
      END TYPE
      or perhaps you can use ASCIIZ null-terminated strings instead
      Last edited by Wayne Diamond; 15 Sep 2014, 04:48 PM.
      -

      Comment


      • #4
        Sorry Wayne, but I NEVER use variable length strings in a UDT of my creation; it's much easier to simply use a string array instead.

        ALL of my UDTs and their fields are of fixed length. That's one of my personal KISS rules.
        It's impossible to make anything foolproof because fools are so ingenious.

        Comment


        • #5
          Apology accepted ...
          It may be KISS though, but it's not overly flexible (hence all your length-related Q's which are impending you by the sounds of it, so perhaps not as KISS as possible)
          Last edited by Wayne Diamond; 15 Sep 2014, 05:15 PM.
          -

          Comment


          • #6
            For instance, if I have a UDT with a 48-byte string field named RecDat.CName and I write data to it directly from a textbox using:
            What %Style did you use to create the textbox? %ES_LEFT, %ES_CENTER, or %ES_RIGHT?
            Do you check for the right kind of data entry or length? You can make sure it's LSET when you do that.
            Rod
            I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

            Comment


            • #7
              Rod,

              To be honest, I didn't check that. All of my text fields are left justified. Many of my numerical fields are right justified, but I convert them to the appropriate numerical type before populating the specific numerical field of the UDT.

              I did check to determine what happened if the left-justified text field was transferred directly to the UDT fixed-length text field and found that, if it was too long, it was truncated to fit the UDT field. If too short, it was padded with zeros. This is consistent with the explanation I finally found buried in help. The experiments confirmed the results for the two instances below:

              If RecDat.LastName = String * 24, then I set RecDat.LastName = "Smith", what happens?
              If RecDat.Data = String * 10, and I set RecDat.Data = "This is way too long!", what happens?

              Nowhere in the Help did I find an example assigning a specific variable to a UDT field. Examples would be appreciated.

              If RecDat.Data = String * 10, and I set RecDat.Data = Space$(5) + "Some of this is gonna get lost!", what would I get?
              It's impossible to make anything foolproof because fools are so ingenious.

              Comment


              • #8
                > If RecDat.Data = String * 10, and I set RecDat.Data = Space$(5) + "Some of this is gonna get lost!", what would I get?

                Code:
                You will get "     Some ". What else you would expect?
                Nothig like trying and see the result.
                Forum: http://www.jose.it-berater.org/smfforum/index.php

                Comment


                • #9
                  > Nowhere in the Help did I find an example assigning a specific variable to a UDT field. Examples would be appreciated.

                  Code:
                  #COMPILE EXE
                  #DIM ALL
                  
                  TYPE Foo
                     x AS LONG
                  END TYPE
                  
                  FUNCTION PBMAIN () AS LONG
                  
                     LOCAL t as Foo
                     LOCAL n AS LONG
                     n = 12345
                     t.x = n
                     ? STR$(t.x)
                  
                  END FUNCTION
                  Read the documentation for TYPE/END TYPE and TYPE SET.
                  Last edited by José Roca; 15 Sep 2014, 10:54 PM.
                  Forum: http://www.jose.it-berater.org/smfforum/index.php

                  Comment


                  • #10
                    > How will I know the text is LSet in the RecDat.CName field?

                    LSET It is the default.

                    Read the documentation for LSET, CSET and RSET.
                    Forum: http://www.jose.it-berater.org/smfforum/index.php

                    Comment


                    • #11
                      Thanks José,

                      I've already run all the experiments and now know what to do.
                      It's impossible to make anything foolproof because fools are so ingenious.

                      Comment


                      • #12
                        Maybe a paragraph with a subject like, "Assignment and Padding" would be useful?

                        e.g I know if you assign a string literal to a STRING * n field ( UDT.string_member = {"string_literal" | string_expression}) , it is an implicit LSET and it's space-padded.

                        I don't know what happens as in post #2 .. CONTROL SET TEXT to UDT.String_Member.

                        So maybe a table like
                        Code:
                        Operation                    Example                                               Result 
                        Direct assgnment         UDT.member ="123"                                 Left-justified, space padded
                        Statement return         CONTROL SET TEXT ..TO UDT.MEMBER         ??? 
                        Function Return           UDT.MEMBER = function (param)                 ??? 
                        Direct Memory move     @pUDT.Member =  variable                        ???
                        Something like that?

                        There may even be subtle differences using STRING and ASCIIZ string destinations (the UDT member).

                        FWIW, I generally wimp out of any defaults and use LSET or TYPE SET.

                        It's a thought.

                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Nowhere in the doc discussion does the rule, "User Defined Types must be defined in your source code outside of any procedures" appear.. It should probably be on the page where it says,
                          The definition of a User-Defined Type begins with the reserved word TYPE and ends with the keywords END TYPE. In between, you define the names and data typesof the member elements (fields) that are to be part of the new Type. For example:...
                          Every user request for help is an opportunity to check the documentation ==> https://forum.powerbasic.com/forum/u...f-function-sub

                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Not that it shouldn't appear elsewhere, but under TYPE/END TYPE it says...

                            TYPE/END TYPE blocks must be defined outside of a Sub, Function, or Class and may be defined only once in any program.
                            "Not my circus, not my monkeys."

                            Comment


                            • #15
                              Just didn't read down far enough.
                              Dale

                              Comment


                              • #16
                                Just didn't read down far enough.
                                Two different pages. User Defined Types, which is this thread and TYPE/END TYPE. Strangely, there is no link to the TYPE/END TYPE or UNION/END UNION page.
                                Rod
                                I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                                Comment


                                • #17
                                  Strangely, there is no link to the TYPE/END TYPE or UNION/END UNION page.
                                  There should be. That's why I never found the "must be outside procedures" thing. I went to the index under "User Defined Types." If I had already known the important word were "TYPE" I probably would have looked there.
                                  Michael Mattias
                                  Tal Systems (retired)
                                  Port Washington WI USA
                                  [email protected]
                                  http://www.talsystems.com

                                  Comment

                                  Working...
                                  X