Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2002-07-17
14
Medium Priority
?
883 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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.

Question has a verified solution.

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

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 …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

636 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