#61059 - 2001-12-04 08:18 PM
UDF flag options
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
YOU WANT OPTIONS!!!! YOU CAN'T HANDLE OPTIONS!!!I have has this idea buzzing around in the back of my head for some time now. one of the problems i have run against when creating a UDF is setting option flags. Typically i have just used the simple OPTIONAL $flag and set it to equal a 1 if true. but this can lead to UDF's like this. THING(1,0,1,1,1,0,1) really nasty in my book. Then I had one of those rare moments when everything becomes clear! Kix has several functions that have multiple options with out having to do the 1,0,1,1,0 dance!!! and *click* i was able to come up with this. ;option flags can be 2,4,8,16,32,64,128,256,512,1024,2048... $option = 4 + 16 + 64 +256 + 2+8 ;a data array of available flags $i = 1 while $i < 50000 $i = $i*2 $flags = $flags + "$i," loop $flags = split($flags,",") redim preserve $flags[ubound($flags)-1]
;sort throught the flag options. ? "the following flag options were set." for each $flag in $flags
if ($option & val($flag)) ? $flag endif next
Now you can have HUNDREDS! of available options in your UDF, with out having to do 1,1,1,0,0,1....  Bryce [ 04 December 2001: Message edited by: Bryce ]
|
|
Top
|
|
|
|
#61062 - 2001-12-04 09:31 PM
Re: UDF flag options
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Bryce, around here ... you shall now be known as "Flag Boy"  Cripes - we already got a flag boy ... never mind ... Excellent Idea (*2) !!! But no need to build UDF's to check options, just use the and & operator and masks, like this:
break on myfunc(2+8+32+1)
function myfunc($flags)
$opt1 = 1 $opt2 = 2 $opt4 = 4 $opt8 = 8 $opt16 = 16 $opt32 = 32 if $flags & $opt1 ?"Option1 selected" endif if $flags & $opt2 ?"Option2 selected" endif if $flags & $opt4 ?"Option4 selected" endif if $flags & $opt8 ?"Option8 selected" endif if $flags & $opt16 ?"Option16 selected" endif if $flags & $opt32 ?"Option32 selected" endif endfunction
Eh ? -Shawn
|
|
Top
|
|
|
|
#61064 - 2001-12-05 12:07 AM
Re: UDF flag options
|
DrillSergeant
MM club member
   
Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
|
Ok, I don't know if I got the subject right, but I saw a Binary calculation party going on here and I couldn't resist crashing in  How's about this?: Options() Action: Converts a decimal number to a binary option string or converts a binary option string to a decimal number or lists the selected options. Syntax: Options(Value, Mode, CheckValue) Parameters: Value: This can be a decimal or a binary string, this depends on the Mode. Mode: 0 = Convert decimal number to binary string 1 = Convert binary string to decimal number CheckValue: If you're not sure of your value, set this value to 1 (true) and you'll get a string returned which sums up the selected options. Returns: A decimal number or binary string or a list of selected options, depending on the mode and the checkvalue. Remarks: Be careful of big numbers (Kix gets confused if you use a decimal number bigger than 2,147,483,647) Dependecies: KiXtart 4.0 (Final) Examples: $test = Options(7,0); will return '111' $test = Options(6,0); will return '011' $test = Options(10001,1); will return '17' $test = Options(10001,1,1); will return 'selected options: 1, 16' $test = Options(13,0,1); will return 'selected options: 1, 4, 8' $test = Options(2 + 16 + 1024,0); will return '01001000001' Source: code:
Function Options($_value, $_mode, optional $_test)DIM $_result, $_ttxt SELECT CASE $_mode=0 $_value=val($_value) $_i=1 While $_value => $_i * 2 $_i = $_i * 2 Loop While $_i > 0 if $_value => $_i If $_test $_ttxt = ', $_i' + $_ttxt Endif $_result = '1' + $_result $_value = $_value - $_i else $_result='0'+$_result endif $_i = $_i / 2 Loop CASE $_mode = 1 $_i = 1 $_result = 0 For $_p = 1 to len($_value) if substr($_value,$_p,1)='1' If $_test $_ttxt = $_ttxt + ', $_i' Endif $_result = $_result + $_i endif $_i = $_i * 2 Next ENDSELECT if $_test $Options = "Selected Options: "+Right($_ttxt,len($_ttxt)-2) else $Options = $_result endif EndFunction
Author(s): DrillSergeant
_________________________
The Code is out there
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|