[Gambas-user] Weird, yet again. Two way access to a collection. (not really a gambas issue)

Christof Thalhofer chrisml at deganius.de
Wed Nov 21 13:40:14 CET 2018


Am 21.11.18 um 12:17 schrieb Bruce:

> I like your general idea here. In fact, though, it's sort of how we are 
> doing this currently.

Ok.

> A real, real example is this. We have an application that lets horse 
> trainers submit a horse for an auction.  There are about 20 gazillion 
> rules and regulations about the animal concerned, which we need to check 
> in that application and several others before the horse is acceptable as 
> as lot in that auction.  So we have a component that does all the 
> necessary checking with the relevant authorities. If everything goes OK 
> then the component can send back a result code of 400 ("Probity checks 
> satisfied, proceed with submission"). Otherwise, it may send back a code 
> of, say 463 ("This horse is registered with &1 as being deceased! Best 
> of luck with trying to sell it") where &1 is replaced with the relevant 
> authority name. The mnemonic in that case :-) might be "REG_HORSE_DEAD". 
> Which is is hell of a lot easier to remember than 462, oops I meant 463, 
> when maintaining the component code. If you get my drift.

Ok. Now the other way round:

At start of your program you can iterate through the constants in modul
"State" from my last mail and dig out the names of the constants.

    Dim Symbols As String[]
    Dim sSymbol as String
    Dim cStates As New Collection
    Dim sKey as String
    Dim vValue as Variant
    Dim stateclass As Class

    stateclass = Object.Class(State)
    Symbols = stateclass.Symbols

    For Each sSymbol In Symbols
        If stateclass[sSymbol].Kind = Class.Constant Then
        	sKey = sSymbol
        	vValue = stateclass[sSymbol].Value
		cStates.Add(sKey, vValue)
        Endif
    Next

(not tested!)

Now you have all the names of the constants together with their bit
position in the collection cStates (store it as global Variable in
State) and can iterate through it if you want to print the key
"REG_HORSE_DEAD" for value 463.

All this is quite ugly but it works. Its ugly because it mixes
functionality with data.

But it is necessary if you want to have the codes at your fingertips in
the editor while programming.

Now you can write

If State.Get(State.REG_HORSE_DEAD) = True then
    Print State.cStates[State.REG_HORSE_DEAD].Key
Endif

(also not tested!)

Alles Gute

Christof Thalhofer

-- 
Dies ist keine Signatur

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20181121/e5c4b83a/attachment.sig>


More information about the User mailing list