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:
Title | Tags | ||
---|---|---|---|
480 | Charity begins at home | Charity work | |
705 | Dreams, obsessions and predictions | DBA, Robert Silverberg | |
706 | Restless legs and a research draft outtake | DBA, Health | |
841 | Investment fund manager | LTC fund management | |
950 | Unconnected snippets | Personal, Shoes | |
1029 | DCI Banks - Wednesday's child | DCI Banks, Police procedurals | |
1316 | Jamie Oliver inspires a chicken dish | Cooking | |
1758 | Been down so long, it looks like up to me | Literature |
No comments:
Post a Comment