Solved

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

Posted on 2002-07-17
14
842 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
Independent Software Vendors: 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!

 
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:AlonHirsch
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 200 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:AlonHirsch
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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 240
fso.FolderExists("\\server\HiddenFolder$") 4 89
Delphi: barcode reading on android platform 1 73
VB script to continue despite error 2 53
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

749 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