473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

I need an array of WithEvents Classes...

I want to have an array of class objects that raise an event when a
condition is true. Like so...
-------------------------------------------------------
Public Class ClientHandler

Public Event DataArrival(ByVal client As TcpClient)
Public Writer As BinaryWriter
Public Reader As BinaryReader
Private myClient As TcpClient

Public Sub New(ByVal client As TcpClient)
myClient = client
Dim Stream As NetworkStream = client.GetStream
Writer = New BinaryWriter(Stream)
Reader = New BinaryReader(Stream)
'Add code here to start a DataArrivalLoop Thread...
End Sub

Private Sub DataArrivalLoop()
Do
If Reader.PeekChar > 0 Then
RaiseEvent DataArrival(myClient)
End If
Loop
End Sub

End Class
--------------------------------------------------------------

Back in the main form code...

Dim WithEvents myClient(10) As ClientHandler '<--Gives an error that says
can't have an array of WithEvents variables.

I want to be able to have up to 10 clients connected and have a single event
handler like this:

Private Sub DataArrival(ByVal client As TcpClient) Handles
ClientHandler.DataArrival
'Do stuff to handle the event...
End Sub

Is this possible? How can I accomplish something like this?
Nov 21 '05 #1
3 4415
You have to programatically wire up the events.

Dim myClient(10) as ClientHandler
for each client as ClientHandler in myClient
AddHandler client, AddressOf DataHandler
next

- Scott Swigart
http://blog.swigartconsulting.com

"Terry Olsen" <to******@hotmail.com> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
I want to have an array of class objects that raise an event when a
condition is true. Like so...
-------------------------------------------------------
Public Class ClientHandler

Public Event DataArrival(ByVal client As TcpClient)
Public Writer As BinaryWriter
Public Reader As BinaryReader
Private myClient As TcpClient

Public Sub New(ByVal client As TcpClient)
myClient = client
Dim Stream As NetworkStream = client.GetStream
Writer = New BinaryWriter(Stream)
Reader = New BinaryReader(Stream)
'Add code here to start a DataArrivalLoop Thread...
End Sub

Private Sub DataArrivalLoop()
Do
If Reader.PeekChar > 0 Then
RaiseEvent DataArrival(myClient)
End If
Loop
End Sub

End Class
--------------------------------------------------------------

Back in the main form code...

Dim WithEvents myClient(10) As ClientHandler '<--Gives an error that says
can't have an array of WithEvents variables.

I want to be able to have up to 10 clients connected and have a single
event handler like this:

Private Sub DataArrival(ByVal client As TcpClient) Handles
ClientHandler.DataArrival
'Do stuff to handle the event...
End Sub

Is this possible? How can I accomplish something like this?

Nov 21 '05 #2
So doing it this way...you don't need the WithEvents in the declaration?
"Scott Swigart" <sc***@3leaf.com> wrote in message
news:O5**************@TK2MSFTNGP09.phx.gbl...
You have to programatically wire up the events.

Dim myClient(10) as ClientHandler
for each client as ClientHandler in myClient
AddHandler client, AddressOf DataHandler
next

- Scott Swigart
http://blog.swigartconsulting.com

"Terry Olsen" <to******@hotmail.com> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
I want to have an array of class objects that raise an event when a
condition is true. Like so...
-------------------------------------------------------
Public Class ClientHandler

Public Event DataArrival(ByVal client As TcpClient)
Public Writer As BinaryWriter
Public Reader As BinaryReader
Private myClient As TcpClient

Public Sub New(ByVal client As TcpClient)
myClient = client
Dim Stream As NetworkStream = client.GetStream
Writer = New BinaryWriter(Stream)
Reader = New BinaryReader(Stream)
'Add code here to start a DataArrivalLoop Thread...
End Sub

Private Sub DataArrivalLoop()
Do
If Reader.PeekChar > 0 Then
RaiseEvent DataArrival(myClient)
End If
Loop
End Sub

End Class
--------------------------------------------------------------

Back in the main form code...

Dim WithEvents myClient(10) As ClientHandler '<--Gives an error that
says can't have an array of WithEvents variables.

I want to be able to have up to 10 clients connected and have a single
event handler like this:

Private Sub DataArrival(ByVal client As TcpClient) Handles
ClientHandler.DataArrival
'Do stuff to handle the event...
End Sub

Is this possible? How can I accomplish something like this?


Nov 21 '05 #3
"Terry Olsen" <to******@hotmail.com> schrieb:
So doing it this way...you don't need the WithEvents in the declaration?


Sure, you don't need it.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jakob Bengtsson | last post by:
Hi, I have a form (which cannot be serialized). In the form's code I declare an object like this (never mind the object nor class name, it's for illustration only): Private WithEvents...
2
by: vivek | last post by:
Hello Every body, I am tring to make an array of server controls with array indes can change dynamically. But I was not able to get the refrence of the control outside the scope in which they...
2
by: student | last post by:
Hi all, I'm having trouble with my datagrid, it is suppose to display a row from a table called "Calls" based on what is selected in the dropdownlist which displays the first and last name from...
2
by: gce | last post by:
What happens : When I first press the button, I get an listbox1 with a,b,c (correct: because of the addtoa(1, "a") addtoa(2, "b") addtoa(3, "c") When I check the checkbox, the eventhandler...
4
by: ThunderMusic | last post by:
Hi, I have a class let's say class1 that raises an event, let's say OnEvent. In another class, let's say class2, I have an array of Class1. I want to be able to trap the events fired by class1...
1
by: GL | last post by:
I am trying to develop a prgram that randomly selects numbers for an addition program, then when the check answer button is pressed it colors the answer text field either green or red depending if...
4
by: c676228 | last post by:
Hi all, In the second web form, I have the following code: (The peopleinfo is a user control which has name and ssn infomation, each field in this user control I have set enableviewstate=true). I...
3
by: Terry Olsen | last post by:
I'm trying to add a domain user to a local group using the code below: Dim LCL As New DirectoryEntry("WinNT://" + Environment.MachineName + ",computer") Dim DOM As New...
4
by: =?Utf-8?B?R3V5IENvaGVu?= | last post by:
Hi all I googled some and found that I can no longer - dim withevents myclassname() I also understand that I can use addhandler... But... what if I do not know the amount of classes that will be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.