MaxStudio Posted June 10, 2009 Share Posted June 10, 2009 Is there a way to predefine a function and label it so I can call it when needed instead of retyping it over and over again? Quote Link to comment
IanH Posted June 10, 2009 Share Posted June 10, 2009 You can create a file which has common functions and $include it into other scripts. However care is needed to make sure that you do not create inter functional dependencies. Quote Link to comment
MaxStudio Posted June 10, 2009 Author Share Posted June 10, 2009 thanks Ian, include would definitely work. Although, i'm looking to do the exact same thing as $include, but contain it all in a single file. any ideas? Thanks for your help. derek Quote Link to comment
IanH Posted June 10, 2009 Share Posted June 10, 2009 Are you saying you are using the same function over and over in the same file? Quote Link to comment
MaxStudio Posted June 10, 2009 Author Share Posted June 10, 2009 (edited) yes exactly i guess it would be a procedure (sub-procedure) rather than a function. Edited June 10, 2009 by MaxStudio Quote Link to comment
MaxStudio Posted June 10, 2009 Author Share Posted June 10, 2009 I got the following test to work using sub-procedures. I get confused about where to put things when there are so many end's... lol Procedure ProSub; VAR id:LONGINT; result:LONGINT; gA, gB: BOOLEAN; A,B,C,D: REAL; FUNCTION DED_ProSub(VAR id:LONGINT):BOOLEAN; BEGIN { initialize dialog creation } id := CreateLayout('Procedure/Sub-Procedure',TRUE,'Create','Cancel'); { Menu 1 } CreateStaticText(id,4,'Type:',-1); CreateCheckBox(id,5,'A.'); CreateCheckBox(id,6,'B.'); { Layout Dialog } SetFirstLayoutItem(id,4); SetRightItem(id, 4, 5, 0, 0); SetRightItem(id, 5, 6, 0, 0); DED_ProSub:=VerifyLayout(id); END; PROCEDURE DD_ProSub(VAR item:LONGINT; data:LONGINT); BEGIN; { SET DEFAULTS } CASE item OF SetupDialogC: BEGIN SetItemEnable(5,TRUE); SetItemEnable(6,TRUE); END; 1:BEGIN IF ItemSel(5) THEN gA:= TRUE ELSE gA:= FALSE; IF ItemSel(6) THEN gB:= TRUE ELSE gB:= FALSE; END; END; END; { Begin Define Sub-Procedures ------------------------------- } Procedure Sub; BEGIN Rect(A,B,C*2,D*2); END; Procedure Sub2; BEGIN Rect(A,B,C*4,D*4); END; { End Define Sub-Procedures -------------------------------- } BEGIN IF DED_ProSub(id) THEN BEGIN result:=RunLayoutDialog(id,DD_ProSub); END; IF (result=1) THEN BEGIN A:=0; B:=0; C:=50; D:=50; IF gA THEN BEGIN Sub2; END; IF gB THEN BEGIN Sub; END; END; END; RUN(ProSub); Quote Link to comment
IanH Posted June 10, 2009 Share Posted June 10, 2009 To avoid confusion with begin and ends, use indentation. There are different styles with this, but all my commercial programming life, I tab (typically 8 spaces) indented after any begin and reverted back before the end. eg. but only using 4 spaces. Programming style is largely personal style so I would format this differently than you have done, but there are some things that are good practice/corporate style rather than just personal style. One thing you could have done is create a single function sub and passed a variable as a scaling factor. Procedure ProSub; VAR id : LONGINT; result : LONGINT; gA, gB : BOOLEAN; A,B,C,D : REAL; FUNCTION DED_ProSub(VAR id:LONGINT):BOOLEAN; BEGIN { initialize dialog creation } id := CreateLayout('Procedure/SubProcedure', TRUE,'Create','Cancel'); { Menu 1 } CreateStaticText(id,4,'Type:',-1); CreateCheckBox(id,5,'A.'); CreateCheckBox(id,6,'B.'); { Layout Dialog } SetFirstLayoutItem(id,4); SetRightItem(id, 4, 5, 0, 0); SetRightItem(id, 5, 6, 0, 0); DED_ProSub:=VerifyLayout(id); END; PROCEDURE DD_ProSub(VAR item:LONGINT; data:LONGINT); BEGIN { SET DEFAULTS } CASE item OF SetupDialogC: BEGIN SetItemEnable(5,TRUE); SetItemEnable(6,TRUE); END; 1: BEGIN IF ItemSel(5) THEN gA:= TRUE ELSE gA:= FALSE; IF ItemSel(6) THEN gB:= TRUE ELSE gB:= FALSE; END; END; END; { Begin Define Sub-Procedures ------------------------------- } Procedure Sub; BEGIN Rect(A,B,C*2,D*2); END; Procedure Sub2; BEGIN Rect(A,B,C*4,D*4); END; { End Define Sub-Procedures -------------------------------- } BEGIN IF DED_ProSub(id) THEN BEGIN result:=RunLayoutDialog(id,DD_ProSub); END; IF (result=1) THEN BEGIN A:=0; B:=0; C:=50; D:=50; IF gA THEN BEGIN Sub2; END; IF gB THEN BEGIN Sub; END; END; END; RUN(ProSub); Quote Link to comment
JBenghiat Posted June 11, 2009 Share Posted June 11, 2009 Forgive me if you already know this, but have you looked at the VS Language Guide? It's available through the VW help system. There's a section called User Defined Functions that you may find useful. -Josh Quote Link to comment
MaxStudio Posted June 11, 2009 Author Share Posted June 11, 2009 Hey Josh! Thanks a lot for pointing out that section in the language guide. "subroutines" is exactly what I was trying to accomplish. Ian, thanks for your help. I'm going to try to keep my scripts more organized using the indentation method you have shared with us. 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.