Saturday, May 10, 2025

Refactoring the checkbox editing code

As I wrote1 yesterday, I'm sure that tomorrow it will be clear how to turn this localised code (similar code appears in another unit) into something more abstract and global. Well, "tomorrow" has come, and in fact it was very simple to refactor the code and create a new ToggleCheckBox procedure.

Procedure ToggleCheckBox (agrid: TDBGrid; sds: TSimpleDataSet); var i, n: integer; s: string; begin s:= agrid.selectedfield.FieldName; for i:= 0 to sds.FieldCount - 1 do if not sds.Fields[i].readonly then if sds.Fields[i].fieldname = s then begin sds.edit; n:= sds.Fields[i].asinteger; if n = 0 then sds.Fields[i].asinteger:= 1 else sds.Fields[i].asinteger:= 0; sds.post; if sds.Eof then begin sds.Prior; sds.Next; end else begin sds.Next; sds.Prior; end; break; end; end;

Several hours later, it occurs to me that it is probably cleaner to pass the grid's selectedfield to the procedure instead of the entire grid; as a result, there is no need for the variable s and the comparison is now against sds.Fields[i] - there's no need to compare the fieldnames.

Internal links
[1] 1935



This day in history:

Blog #Date TitleTags
48010/05/2012Charity begins at homeCharity work
70510/05/2014Dreams, obsessions and predictionsDBA, Robert Silverberg
70610/05/2014Restless legs and a research draft outtakeDBA, Health
84110/05/2015Investment fund managerLTC fund management
95010/05/2016Unconnected snippetsPersonal, Shoes
102910/05/2017DCI Banks - Wednesday's childDCI Banks, Police procedurals
131610/05/2020Jamie Oliver inspires a chicken dishCooking
175810/05/2024Been down so long, it looks like up to meLiterature

No comments: