James Russell Posted June 4, 2013 Share Posted June 4, 2013 Hey all, I have a script which looks great but has little execution. I have a lovely dialog which has 10 checkboxes. When each of these are checked or not checked they save their data as a Boolean to 10 different Booleans labelled, ChkA, ChkB all the way to ChkJ. My question is what is the easiest way I can tally these up to find out how many are checked? I don't need to know which ones are checked just the total overall of how many are checked out of the ten and pop that number into an integer. Happy coding! J Quote Link to comment
Hippocode Posted June 4, 2013 Share Posted June 4, 2013 (edited) instead of ChkA you could use an array 1 to 10 to store the booleans. Let a counter go from 1 To 10 that loads a function with the array. This funcion will increase 1 to another counter if the value in the array is true. Edited June 4, 2013 by hippothamus Quote Link to comment
Miguel Barrera Posted June 4, 2013 Share Posted June 4, 2013 Instead of saving as boolean, you could save as integers. 0 for false and 1 for true. To find the total checked you just need to add all the variables. Quote Link to comment
JBenghiat Posted June 4, 2013 Share Posted June 4, 2013 Many programming languages can access a boolean as a 1 or 0 (which is how the value stores in memory), but Pascal only lets you access the key words. You can write a simple function to convert a boolean to an integer. PROCEDURE MyScript; VAR bool1, bool2, bool3 :BOOLEAN; {---------------------------------------------------------------------} FUNCTION Bool2Int(inBool :BOOLEAN) : INTEGER; BEGIN CASE inBool OF TRUE: Bool2Int:=1; FALSE: Bool2Int:=0; END; {CASE} END; {---------------------------------------------------------------------} BEGIN bool1:=TRUE; bool2:=FALSE; bool3:=TRUE; Message(Bool2Int(bool1)+Bool2Int(bool2)+Bool2Int(bool3)); END; Run(MyScript); Quote Link to comment
MullinRJ Posted June 4, 2013 Share Posted June 4, 2013 You can also create a function B2I (short for Boolean_To_Integer), which will return 1 for True and 0 for False. function B2I (Boo :Boolean) :Integer; { Return 1 for True and 0 for False. } Begin ???if Boo then?B2I := 1; End; Then, in your main program add them up with: Cnt := B2I(ChkA) + B2I(ChkB) + B2I(ChkC) + B2I(ChkD) + B2I(ChkE) + B2I(ChkF) + B2I(ChkG) + B2I(ChkH) + B2I(ChkI) + B2I(ChkJ); HTH, Raymond Quote Link to comment
MullinRJ Posted June 4, 2013 Share Posted June 4, 2013 Oops, I should have sent mine before I left the room. Same as Josh said. Quote Link to comment
James Russell Posted June 5, 2013 Author Share Posted June 5, 2013 (edited) Thanks for all the great replies guys! Late last night I managed to convert Booleans to Integers but hadn't yet linked that to a function. This is the solution! Cheers, J Edited June 6, 2013 by James Russell Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.