?
Solved

How to "new" Winsock in a class module???

Posted on 2002-07-17
14
Medium Priority
?
888 Views
Last Modified: 2013-11-13
Private Sub Class_Initialize()
   Set theSock = New Winsock   <--- Error
End Sub

The error: "Compile Error: Invaild use of New keyword"

I can't "new" Winsock in a class module. As you know, when you type "New" in VB, VB will automatically popup a listbox for all objects that can be "new". But I can't see Winsock appeared on the listbox.

Also, I have placed another Winsock control on the Main Form, don't know that will this cause the problem.
How can I fix this problem? Please Help!!!


raymng
0
Comment
Question by:Raymond
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7159588
You cannot initiate a Winsock instance like this, you have to load a new winsock control to an existing form.

So, you have to create a winsock control (say name=winsock)on the form, with index=0. If you need another winsock control, you need to do like this:
dim theSock as control
load winsock(1)
set theSock = winsock(1)
...

to "unload" it, use this code (in the form)
set theSock = nothing
unload winsock(1)

As you have a class module, that class module will have to refer to the form to do the operations of loading and unloading the winsock controls...

CHeers
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7159614
You have to set a reference to the winsock dll via Project->REFERENCES and not Project->Components
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7159618
Sorry, a reference to the winsock ocx, not dll.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 28

Expert Comment

by:AzraSound
ID: 7159624
Here is a thread discussing this idea and some of the implications of doing it this way:
http://www.vbip.com/forum/topic.asp?id=17
0
 
LVL 4

Expert Comment

by:Alon Hirsch
ID: 7159631
What exactly are you trying to do ?
If you want to create a control array of winsock controls use Load wscSock(iIndex) where iIndex is the next available number.

The Winsock class within the control is declared Public - Not Creatable. This means that you can reference the class but can not create NEW instances of it without creating new instances of the control that contains it.

HTH,
Alon
0
 

Author Comment

by:Raymond
ID: 7159636
angelIII:

''''''''''''''''''''''''''''''
dim theSock as control
load winsock(1)
set theSock = winsock(1)
...

to "unload" it, use this code (in the form)
set theSock = nothing
unload winsock(1)
'''''''''''''''''''''''''''''
Whether should this code be written on the Class Module or the Main Form?

Does it mean I can only place one Winsock on the Form? so if I need more Winsock, then I need to create it by coding?

I also don't understand "As you have a class module, that class module will have to refer to the form to do the operations of loading and unloading the winsock controls", can you give me some sample coding?

Thank you very much!

raymng
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 800 total points
ID: 7159660
The code MUST go to the form where the winsock control is. The problem is that the winsock control requires a form to be loaded...

no, you can place several controls on 1 form. But to load a control dynamically, you need to use the control array (using index, LOAD and UNLOAD)

The class code will have to hold a reference to the form with the winsock control(s). I do typically use a dedicated form for this, in order to avoid any troubles with multiple forms. You could also have a reference of that form in a global variable (public gWinsockForm ... in a standard module). I once have written this kind of thing, but obviously it was at a client site, and the client holds the sources...
In short, I controlled the winsocked controls by the form dedicated to it, with functions like AddSocket(), GetSocket(n) and ReleaseSocket(n), where AddSocket returned the index for the assigned socket control, which needs to be passed to GetSocket(n) and ReleaseSocket(n).
The functions SendData(n, data) and the event RecievedData(n, data) complete the handling...

CHeers

0
 

Author Comment

by:Raymond
ID: 7159683
AzraSound:

I don't have winsock.ocx on my system, where can I donwload it?


raymng
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7159728
Its called mswinsck.ocx
0
 
LVL 4

Expert Comment

by:Alon Hirsch
ID: 7159742
Hi,

The file is called Mswinsck.ocx

Alon
0
 

Author Comment

by:Raymond
ID: 7159789
When I tried to add this reference, error come again: "Name conflicts with existing module, project, or object library"


raymng
0
 

Author Comment

by:Raymond
ID: 7160238
Thanx everyone here!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7160272
>>When I tried to add this reference, error come again: "Name conflicts with existing module, project, or object library"

Because you had it selected via Project->Components, not Project->References.  You needed to remove if from the Components first and then set it as a reference.
0
 

Author Comment

by:Raymond
ID: 7160321
AzraSound:

Thank you! I have solved this problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question