Tech Note 42: Using the Dictionary LibraryJanuary 19, 2009© NSB Corporation. All rights reserved. |
NSBDictLib implements a key/value dictionary container object. It is useful for storing named variables, preferences, limited user input etc., so they can be rapidly accessed. All stored entries are in the Dynamic Heap, so they can't be too big.
This can be useful for storing values between forms or loading initial settings. Another use would be to keep a set of string values depending on language (English, German, Japanese, etc), and use it to load captions and titles in your app.
To use it, add NSBDictLib.prc to your project as a resource. It is a small file, about 4k in size. Initialise it in your Startup code as follows:
loadLibrary "NSDictLib", "NSDL" 'case is importantTo see how each of these functions works, see the NSBDictLib sample project.
Files included:
NSBDictLib.INF | The info file for NSBDictLib. Put this in your \nsbasic\lib folder. |
NSBDictLib.prc | The library. Put this into your \nsbasic\lib folder and add it to your project. |
Dictionary.prj | A sample project that demonstrates the functions. |
' Create a new dictionary object; the library can produce and operate over multiple dictionaries ' which is very convenient Dim ref as Integer ref = NSDL.NewDict()
NSDL.FreeDict(ref)
NSDL.SetKeyVal(ref, "Key1", "Value1")
varpref = NSDL.GetKeyVal(ref, "Key1")
NSDL.DelKey(ref, "Key1")
NumberOfKeys = NSDL.count(ref)
key12 = NSDL.GetKeyAt(ref, 12)
val12 = NSDL.GetValAt(ref, 12)
NSDL.Clear(ref)
Dim in as String in = "A=Cartman|B=Kenny|C=Chef" NSDL.FromString(ref, in, "|")This sample creates 3 keys ("A", "B", "C") with the values "Cartman", "Kenny" and "Chef" respectively.
Dim out as String out = NSDL.ToString(ref, "|")The string, out, will now have the value "A=Cartman|B=Kenny|C=Chef"