Data

Introduction

PureBasic allows the use of Data, to store predefined blocks of information inside of your program. This is very useful for default values of a program (language string for example) or, in a game, to define the sprite way to follow (precalculated).

DataSection must be called first to indicate than a data section follow. This mean than all labels, and data component will be stored in the data section of the program, which has a much faster access than the code section. Data will be used to enter the data. EndDataSection must be specified if some code follows the data declaration. One of good stuff is you can define different Data sections in the code without any problem. Restore and Read command will be used to retrieve the data.

Commands


Syntax
DataSection
Description
Start a data section.

Syntax
EndDataSection
Description
End a data section.

Syntax
Data.TypeName
Description
Defines data. The type can only be a native basic type (long, word, byte, float, double, quad, character, string). Any number of data can be put on the same line, each one delimited with a comma ','.

Example:

  Data.l 100, 200, -250, -452, 145
  Data.s "Hello", "This", "is ", "What ?"
For advanced programmers: it's also possible to put a procedure address or a label address inside Data when its type is set to long (.l). This can be used to build easy virtual function tables for example.

Example:

  Procedure Max(Number, Number2)
  EndProcedure
  
  Label:
    
  DataSection
    Data.l ?Label, @Max()
  EndDataSection

Example:

  Interface MyObject
    DoThis()
    DoThat()
  EndInterface

  Procedure This(*Self)
    MessageRequester("MyObject", "This")
  EndProcedure

  Procedure That(*Self)
    MessageRequester("MyObject", "That")
  EndProcedure

  m.MyObject = ?VTable

  m\DoThis()
  m\DoThat()


  DataSection
    VTable:
      Data.l ?Procedures
    Procedures:
      Data.l @This(), @That()
  EndDataSection

Syntax
Restore label
Description
This keyword is useful to set the start indicator for the Read to a specified label.

Example:

  Restore StringData
  Read MyFirstData$
  Read MySecondData$
  
  Restore NumericalData
  Read a
  Read b

  Debug MyFirstData$
  Debug a
  
  End  
  
  DataSection
    NumericalData:    
      Data.l 100, 200, -250, -452, 145
      
    StringData:
      Data.s "Hello", "This", "is ", "What ?"

Syntax
Read
Description
Read the next available data. The next available data can be changed by using the Restore command. By default, the next available data is the first data declared.