PDA

View Full Version : Do you know: Logical variables



ErosOlmi
06-05-2011, 12:20
Zak has asked (thanks a lot for this) to improve Split function in order to split a string buffer into its single characters when String Delimiter is null. See request: http://www.thinbasic.com/community/project.php?issueid=276

I've already implemented it and it will be present in next release but in the meantime I've taken this request as an excuse to refresh the possibility to use thinBasic logical variables when you need to give a different meaning to a an already allocated memory area of ... whatever.

The following example illustrates a way to consider each single byte of a dynamic string an element of a byte array and an element of a single char at the same time.

MyArrayOfChars and MyArrayOfBytes arrays are logical arrays because using the AT clause of the DIM keyword they share the same memory location of sBuffer string.

Important is to remember that when you change the content of a logical variable you change the content of the shared memory but when a logical variable is destroyed (because, for example, out of scope) original shared memory remain there.

Ciao
Eros




Uses "console"
Dim sBuffer As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
Dim MyArrayOfChars(Len(sBuffer)) As String * 1 At StrPtr(sBuffer)
Dim MyArrayOfBytes(Len(sBuffer)) As Byte At StrPtr(sBuffer)
Dim lCounter As Long

For lCounter = 1 To UBound(MyArrayOfChars)
PrintL Format$(lCounter, "000") & " " & MyArrayOfChars(lCounter) & " " & MyArrayOfBytes(lCounter)
Next
WaitKey

zak
06-05-2011, 17:27
logical arrays ....
this is a very usefull example, thank you Eros
zak