KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Addressing BLOB contents
PRODUCT: 4D | VERSION: | PLATFORM:
Published On: October 27, 2000

If you're not used to using BLOBs, one of the most important things to understand is that you should NEVER use an invalid BLOB address.
Let's take this example:
$charCode := MyBLOB {$address}
Here, the code is assigning to the variable $charCode the value of one of the bytes in the BLOB MyBLOB. The particular byte is specified by the variable $address inside the curly braces.
Making sure that the value of $address is valid is critical.
Let's suppose the BLOB is 100 bytes long. The range of valid address values will start with 0, which represents the first byte in the BLOB. Therefore, 99, which is the length of the BLOB minus 1, will be the last valid address value. So you need to make sure that before you run that line of code, the address value specified is somewhere in the range of 0-99. Any other value, especially in compiled code, can cause a crash, because you've asked for the value of a location in memory that doesn't exist.
You can also use a calculated value for a BLOB address:
$charCode := MyBLOB {$offset+$i}
In this example, the variable $offset could hold the beginning address of a line of text within a BLOB, to which you add the index $i to reference a particular character in that line of text. When you use a calculated BLOB address, it is essential that you make sure that the expression inside the curly braces evaluates into a valid value within the range of the BLOB's size.
For example, if your BLOB is empty, what would be the range of valid index values? None.
Because zero would be referring to the first byte of the BLOB, and there is no first byte. So if the BLOB is empty, you shouldn't even be executing a line of code like:
$charCode := MyBLOB {$address}
Instead, you should always (especially when using calculated values) check for valid range, with code such as:
Case of
:(BLOB size (MyBLOB) = 0)
$charCode := -1

:($address < 0)
$charCode := -1

:($address >= BLOB size (MyBlob))
$charCode := -1

Else
$charCode := MyBLOB{$address}
End case
In this example, the error code -1 is returned in the variable $charCode if the BLOB address specified in $address is invalid.