Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reusing code

Status
Not open for further replies.

ajpa

Programmer
Jan 31, 2002
52
GB
I'm afraid I'm still very new to OOP, and I expect this question has something to do with Classes, which still seem a bit hard!

In FoxPro 2 you could put often-used code snippets, UDFs and things into a separate file which you referenced with SET LIBRARY TO <filename>. Then you could call those snippets from anywhere in the application.

How do you achieve the same effect in Visual FoxPro ?

ajpa
 
This is somewhat difficult since OOP in VFP takess some time to get use to, and I suggest you first try to read some books on this subject, since this would make thing much clearer.
I could give you a long and insightfull lecture here in the forum, but I sugest you read some books on the subject first.

There have been some threads about books on VFP in this forum. Just do the keyword search on VFP books.

Shortly:
Reusing code in VFP can be very easily done by using baseclasses, subclasses and inheritance.

HTH,
Weedz (Edward W.F. Veld)
My private project:Download the CrownBase source code !!
 
I was afraid you'd say that. There was just an off-chance that it might be easy! Never mind. I'll do some reading.

Thanks

ajpa
 
You can still do the same as you used to.. only VFP makes a distinction between SET LIBRARY TO and SET PROCEDURE TO... now you use SET LIBR ONLY for .FLL files, which is no big deal, since you can now SET PROCEDURE TO proga then SET PROCEDURE TO progb ADDITIVE
 
On the other hand, if you simply want to have a program with a lot of procedures and functions in it, just create a program with various things in it similar to

Code:
 PROCEDURE SayHi
    LPARAMETERS myName
? &quot;Hello, &quot; + myname
ENDPROC

If this is one of a number of procedures in MyProcs.prg then before you use it do

Code:
SET PROCEDURE TO MyProcs ADDITIVE

and when you want to run it either do it as a procedure

Code:
DO SayHi WITH &quot;ajpa&quot;

or as a function

Code:
 DummyVar = SayHi(&quot;ajpa&quot;)

(Of course if you're using it as a function you'd normally RETURN a value to DummyVar rather than just print it to the screen. Dave Dardinger
 
Don't forget about form methods.

Form->New Method
Hello

in hello:
LPARAMETERS MyName

WAIT WINDOW 'Hello ' + MyName NOWAIT
RETURN

In the form somewhere:
ThisForm.Hello('Dave')

Dave S.
 
Except, of course, that if you're using form methods to 'reuse' code, you're probably going to subclass your form so that these methods will be available in all forms created with this subclass.

And, equally of course, as weedz might point out, the more OOP way of doing things would be to create a procedures object (aka Business Object), which would have all the procedures you want built in as methods. Dave Dardinger
 
In which case, is there an easy way to create and include the Business object in the project ?

Thanks for all these suggestions,

ajpa
 
Several ways:

Create it visually, store it in a class library (.vcx),
then in your main Prog, issue:
Code:
SET CLASSLIB TO Myclasslib ADDITIVE
oProcs = CREATE('MyProcClass')
* Or just:
oProcs = NEWOBJECT('MyProcClass','MyClassLib')
or create it in code in a procedure file:
Code:
SET PROCEDURE TO MyProcFile ADDITIVE
oProcs = CREATE('MyProcClass')
(Of course, if you create it in code, and use SET PROCEDURE TO to access the class in the proc file, you ALSO get access to all the &quot;old style&quot; regular Procedures in that file, too! ;)
 
By the way, if you haven't started doing it already, take all of the VFP base classes, subclass them, add them to a class library you create and use only them (or subclasses of them) in all the projects you create. This lets you go back and make changes to all your projects at once, whereas if you use the base classes, and later decide you want to add a new property or method to all your textboxes, for instance, you have to go back an make the change to every instance of that class by hand. No fun, believe me. Most developers make subclasses of their subclasses for every major project too so that they can make changes which just affect a given project.

Anyway, you'll probably want to make your procedure class based on the 'custom' class, though I know some guys who make nonvisible classes like them based on the seperator class since it has very low overhead. But unless you're distributing your app on a floppy disk, I don't see where it makes much difference. So make up a list of all the procedures you want in your class and then type something like:

Code:
MODIFY CLASS myProcClass OF myClassLib AS myCustom FROM myClassLib

Now go to the tool bar to Class/New Method and enter the names of all of them in. With that done you can type code in the new methods or cut and paste from existing code.

Dave Dardinger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top