• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

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

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
Raymond
Asked:
Raymond
  • 5
  • 5
  • 2
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
AzraSoundCommented:
You have to set a reference to the winsock dll via Project->REFERENCES and not Project->Components
0
 
AzraSoundCommented:
Sorry, a reference to the winsock ocx, not dll.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
AzraSoundCommented:
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
 
Alon HirschSoftware Development ManagerCommented:
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
 
RaymondAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
RaymondAuthor Commented:
AzraSound:

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


raymng
0
 
AzraSoundCommented:
Its called mswinsck.ocx
0
 
Alon HirschSoftware Development ManagerCommented:
Hi,

The file is called Mswinsck.ocx

Alon
0
 
RaymondAuthor Commented:
When I tried to add this reference, error come again: "Name conflicts with existing module, project, or object library"


raymng
0
 
RaymondAuthor Commented:
Thanx everyone here!
0
 
AzraSoundCommented:
>>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
 
RaymondAuthor Commented:
AzraSound:

Thank you! I have solved this problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now