Jump to content
Developer Wiki and Function Reference Links ×

Count Relating to Multiple Variables


Recommended Posts

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

Link to comment

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 by hippothamus
Link to comment

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);

Link to comment

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

Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...