Geoffrey Callaghan

I am attempting to write some functionality using the winsock control with VFP 6.0. I have a pretty good understanding of winsock itself, but I'm having trouble accessing the object from my code. So here are my two options, neither of which I can figure out right now:

1) The object is defined on a form, but needs to be accessed from code in a .prg. I have never been able to figure out how to access an object on a form from a .prg in FoxPro. In VB, no problem, but in FoxPro, if I say form1.objectname.member, it says it doesn't know what form1 is.

2) I can create the object in the .prg (which I would prefer). I have no idea how to go about this in FoxPro.

I am currently attempting to do the whole thing in a form, but this solution will be inelegant considering the original design. Any ideas

Geoff Callaghan

Re: One basic question, one not so basic


Usually I put the code in a init method of a form:


IP = CREATEOBJECT("MSWinsock.Winsock")

I never had problames to to call some parameters like:

IPAddress = IP.LocalIP


or other from a procedure or another form.

Re: One basic question, one not so basic

Alex Feldstein

Whenever you have code that is common to many routines (i.e. can be called from different places), you can place it in a PRG or in a common object (e.g. oAPP) that is instantiated early in your calling hierarchy.


oAPP = CreateObject("MyApplicationWideObject")

This object will have behavior (methods) and data (properties) to give it the functionality you need.

If the routine you need in your toolbox will be called from forms and sometimes from running priograms (e.g. prg), then it's better to place it as a FUNCTION or PROCEDURE inside a prg , which is just a text file with a prg extension and VFP code in it.

At the beginning of your program, you can use XBASE code to do a SET PROCEDURE TO MyUtilities.prg and then all its functionality will be available to you as if it were part of the language.

Note that code created in a prg does not have a private datasession, whereas code instantiated in an object can, if the base class does have it (e.g. Session)

To answer your other question, to call a form's method from a program, you need a reference to the form. You cannot use ThisForm as the prg does not live in the form.

What you do is take a reference to the active form like this:

oForm = _screen.ActiveForm

then you can do:



myvariable = oForm.MyProperty


To create an object oriented program in a PRG you sse the DEFINE CLASS statement.

There is always the question if it is better to store classes in PRGs or VCXs (forms and other objects). To see the pros and cons of each approach look at: Wiki~PRGvsVCX


Re: One basic question, one not so basic

Geoffrey Callaghan

Thanks, guys! Both answers were helpful, for different reasons. Now I have a followup or two:

1) I created the object in my .prg, which is how I wanted to do it all along. That worked fine. However, when I do a connect, the state never reaches 7 (connected). It stays on 6(connecting) until it reaches 9(error). This code is the same as I was using in a form, and it worked fine there. Here's what I have:

Wsock.remotehost = ""

Wsock.remoteport = 900


2) Even when I'm connected using this code in a form, data I send shows up on the other end as gibberish. However, when I send data back, it works perfectly. It looks like it is sending in one baud rate, but answering in another...that doesn't make sense, does it

Re: One basic question, one not so basic

Geoffrey Callaghan

Never mind. I got it. Thanks, guys.