From Xojo Documentation

Revision as of 19:21, 14 August 2018 by PLefebvre (talk | contribs) (Created page with "{{ClassBox | super=Object | scope=global }} {{Description | text=A read-only class for managing blocks of memory. Since the contents of a MemoryBlock cannot be altered, if...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Class (inherits from Object)

A read-only class for managing blocks of memory. Since the contents of a MemoryBlock cannot be altered, if you need to alter a MemoryBlock, use MutableMemoryBlock.

Data fa-lock-32.png LittleEndian Size fa-lock-32.png
BooleanValue Int32Value Right
CStringValue Int64Value SingleValue
Clone Int8Value UInt16Value
CurrencyValue Left UInt32Value
DoubleValue MethodName UInt64Value
IndexOf Mid UInt8Value
Int16Value PtrValue

Constructor(bytes() As Byte)

Constructor(other As MemoryBlock)

Constructor(p As Ptr)

Constructor(p As Ptr, size As UInteger)

Constructor(size As UInteger)


For 32-bit apps, you can request a maximum of about 2 to 3 GB depending on the OS. Generally Windows is closer to 2GB and macOS/Linux are closer to 3GB. There is no practical limit for 64-bit apps.

To convert Text to a MemoryBlock, use TextEncoding.ConvertTextToData. To convert a MemoryBlock to Text, use TextEncoding.ConvertDataToText.

Compared to MutableMemoryBlock, MemoryBlock is more efficient in terms of memory use savings, but doesn't really have any difference in terms of access. Being able to pass around immutable chunks of data allows the framework to directly reference internal data and avoid having to copy it on the off chance it might get modified.


To convert a hex Text to a MemoryBlock you can use code like this:

// hex contains hex as Text
Dim bytes() As UInt8
For i As Integer = 0 To hex.Length - 2 Step 2
Dim value As UInt8 = UInt8.FromHex(hex.Mid(i, 2))
Dim mb As New Xojo.Core.MemoryBlock(bytes)


To EncodeHex the contents of a MemoryBlock you can use code like this:

Dim joiner() As Text
For i As Integer = 0 To hashMB.Size - 1
Dim b As UInt8 = hashMB.UInt8Value(i)
Dim hex As Text = Text.Join(joiner, "")

Converting to a Classic MemoryBlock

When using both the Classic and New Framework in a single (desktop, web or console) app, you may find that you need to convert a Xojo.Core.MemoryBlock to a Classic MemoryBlock for use with other methods. You can do so by copying the data from the Xojo.Core.MemoryBlock to the Classic MemoryBlock:

// newMB is a Xojo.Core.MemoryBlock
Dim temp As MemoryBlock = newMb.Data
Dim mb As New MemoryBlock(newMb.Size)
mb.StringValue(0, mb.Size) = temp.StringValue(0, mb.Size)
temp = Nil // optional as it will also be cleared when it goes out of scope

See Also