Type
Type <.typeName.> [As <.typeName.>] ... EndType
 
Parameters: NONE
Returns: NONE
 

      You can define your own data types with Type declarations. User defined types in PlayBASIC are very similar to Structs in C/C++ or Records in Pascal and contain one or more Fields.

      A simple type definition might look like this:

  
  Type MyType
     Field1
     Field2#
     Field3$
     ArrayField4(100)
     ArrayField5#(200)
     ArrayField6$(300)
  EndType
  


      This declaration block defines a type called MyType that contains six fields. An integer field called Field1, a float field called Field2#, a string field called Field3$, a static Integer array field called ArrayField4(), a static float array field called ArrayField5#(), and static String array field called ArrayField6$()

      Note: In the example above we've used the short cut declarations. What's that ? - Well, to indicate the data type of each field we're using the postfix symbols. So a string field uses the $ symbol in the field name, and Floats use their # postfix. This is a handy short cut for basic datatypes such as Integers,Floats and String. But you can also use long hand declarations (Which is in some situations is required!), which use the "as datatype" convention.

      For example,

  
  Type MyType
     Field1 As Integer
     Field2 As Float
     Field3 As String
     Field4 As Byte
     Field6 As Word
     ArrayField4(100)
     ArrayField5#(200)
     ArrayField6$(300)
  EndType
  


      In this example we're added long hand declaratiosn to some of the fields. These allow us to specificly define what each field should be.





      In order to use this type you need to delcare a variable with this type:


  
  Dim MyNewVar As MyType
  


You can also declare an array where each cell stores it's own set of MyType fields

  
  Dim MyNewArray(10As MyType
  



     To access each defined type field of the new variable you type the variable name followed by a dot (.) and then the field name:

  
  MyNewVar.Field1 = 42
  MyNewVar.Field2# = 1.2345
  MyNewVar.Field$ = "Play Basic"
  
  MyNewArray(4).Field1 = 456
  



     Each field of a type can either be of a basic type (Integer, Float or String) or a previously user defined type:

  
  Type TPoint
     X
     Y
  EndType
  
  Type TPlayer
     Name$
     Position As TPoint
     Health
  EndType
  
  Dim MyPlayer As TPlayer
  
  MyPlayer.Name$ = "Hero"
  MyPlayer.Position.X = 12
  MyPlayer.Position.Y = 200
  MyPlayer.Health = 100
  
  



     PlayBASIC offers another feature called Inherited Types. That means that you can inherit the fields of a previously defined type in a new type.
Example:

  
  Type TNameAge
     Name$
     Age
  EndType
  
  Type TAllDetails As TNameAge
     Address$
     FavouriteColour$
     Weight#
  EndType
  


     With As statement the type "TAllDetails" inherits all the fields of "TNameAge". Therefore the type "TAllDetails" now has the following fields:

     Name$
     Age
     Address$
     FavouriteColour$
     Weight#



      To learn more about TYPES please read the TYPES Tutorial


 
Related Info: Dim | EndType | For | LinkedLists | Pointer | TypeOf | Types :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com