Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Storing a number of power of 2 into INT field
Message
From
12/06/2017 12:55:29
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01651872
Message ID:
01651930
Views:
65
>>You could make a Character(30) field with each position representing a Feature. Then you can check the Feature with substr(Features,1,1) = "Y", Substr(Features,2,1) = "Y" etc.
>
>This is exactly how this feature works in my app now. And I want to change it; no urgent need but I feel it should be improved.

If you currently work only at that abstraction level, this will eliminate 84% of your flagstring length, but be less approachable in debugger.
uses Python idea of "__" prefixing things internal to working, marking a "stay away" in normal code
uses empty() in set/get so you are not forced to use boolean variables, if they originate in numerics...
//upd: added untested flipflag, before you code this.setflag(tnNum, !this.getflag(m.tnNum))
//upd: added test for flipflag, cleanup and beautify
;-))
clea

local loF
loF = createobject("flag_handler")
loF.__TstClass()

define class flag_handler as custom
	__cFlagchar = 0h + space(40)
	__lRecChanged = .f.  && to check if replacing in record needs to be done on skipping

	function init()
		*** slower, but MUCH cleaner, as it uses only official user methods
		*** instead of bitclear(this.__cFlagchar,0, LEN(this.__cFlagchar)*8)
		for lnRun = 1 to len(this.__cFlagchar)*8
			this.SetFlag(m.lnRun, .f.)
		next
		this.__lRecChanged = .f.
		return dodefault()

	function GetFlag(tnFlagNum)
		return !empty(bittest(this.__cFlagchar, m.tnFlagNum - 1))    && 0-based this.__cFlagChar as in C usually happening

	function FlipFlag(tnFlagNum)
		this.__lRecChanged = .t.
		this.__cFlagchar = bitnot(this.__cFlagchar, m.tnFlagNum - 1)

	function SetFlag(tnFlagNum, tlFlag)
		this.__lRecChanged = .t.
		if empty(m.tlFlag)
			this.__cFlagchar = bitclear(this.__cFlagchar, m.tnFlagNum - 1)
		else
			this.__cFlagchar = bitset(this.__cFlagchar, m.tnFlagNum - 1)
		endif

	function __ChkFlag(tnFlagNum, tlShouldBe)
		local llErg
		llErg = this.GetFlag(m.tnFlagNum)
		if pcount()=1
			? m.tnFlagNum, m.llErg
		else
			? m.tnFlagNum, m.llErg, m.tlShouldBe
		endif

	function __TstClass()

		local lnTest1, lnTest2, llFlag1, llFlag2
		lnTest1 = 22
		lnTest2 = 42

		= this.__ChkFlag(m.lnTest1)
		= this.__ChkFlag(m.lnTest2)

		llFlag1 = .t.
		= this.SetFlag(m.lnTest1, m.llFlag1)
		= this.__ChkFlag(m.lnTest1, m.llFlag1)

		= this.SetFlag(m.lnTest1, m.llFlag2)
		= this.__ChkFlag(m.lnTest1, m.llFlag2)

		= this.SetFlag(m.lnTest2, m.llFlag1)
		= this.__ChkFlag(m.lnTest2)

		= this.__ChkFlag(m.lnTest1)
		= this.FlipFlag(m.lnTest1)
		= this.__ChkFlag(m.lnTest1)
		= this.FlipFlag(m.lnTest1)
		= this.__ChkFlag(m.lnTest1)

enddefine
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform