Delphi - connecting similar procedures -
i working on 1 little bit complex application , have found 1 problem. going try reduce complexity of application 1 simple application, example. point is, have 16 regions , there variables , procedures each 1 of them. each procedure have universal each region. resolve writing 1 "if" on beginning of procedure , them copy 15 times bellow , changing little bit, because each region makes difference in few words. have change word in each 1 of them. makes code sooo unclear , wasting of time. there way, how write 16 "ifs" one? using template or that?
example application:
key code:
procedure tform1.writeitem; var item:integer; begin if currentfile='firstfile' begin seek(firstfile,filesize(firstfile)-1); read(firstfile,item); inc(item); write(firstfile,item); end; if currentfile='secondfile' begin seek(secondfile,filesize(secondfile)-1); read(secondfile,item); inc(item); write(secondfile,item); end; end;
full version:
https://drive.google.com/drive/folders/0bzhr4bza5ibuazjux0fwqzbxchm?usp=sharing
i guessing firstfile, secondfile , on of type tfile or descendant of it, first change make make 'currentfile' of same type (or ancestor of it). then, instead of setting currentfile string, put like
currentfile := fifthfile;
for instance.
then procedure becomes
procedure tform1.writeitem; var item:integer; begin seek(currentfile,filesize(currentfilefile)-1); read(currentfile,item); inc(item); write(currentfile,item); end;
better, though, pass file parameter, this
procedure tform1.writeitem( const curremtfile : tyourfiletype); var item:integer; begin seek(currentfile,filesize(currentfilefile)-1); read(currentfile,item); inc(item); write(currentfile,item); end;
edit
as pointed out in comments, procedure not required object variables (although real procedure may). can make independent of object 1 of 2 ways: either move function out of object altogether
procedure writeitem( const curremtfile : tyourfiletype);
or make class procedure
class procedure tform1.writeitem( const curremtfile : tyourfiletype);
as general principle prefer latter method, move different class designed handle type of functionality (not form).
Comments
Post a Comment