|
-
Nov 23rd, 2006, 04:23 PM
#1
Thread Starter
Frenzied Member
Need object array or objects in a collection with events
I can't get my head around the way .net does control arrays.
I have two user controls(winsocks) in the form tray, one for listening for connections at all times. And the other will accept a client, when something connects to the first socket. So i need the second sock as a control array or some type to allow multiple clients to be connected. It needs to create a clone of winsock2 at runtime which will then accept the client
How can I do this in .net? In VB6 it was easy, just a simple control array. I need for the code in the events to apply to all the new winsocks that are created at runtime.
Thanks all
-
Nov 23rd, 2006, 05:03 PM
#2
Re: Need object array or objects in a collection with events
The reason that you can't get your head around how .NET does control arrays is because it doesn't, so don't even try. You need to stop trying to get VB.NET to work the way VB6 did and just use VB.NET as it was intended.
If you want to create an object in code then just go ahead and create an object. If you want to put that object in an array or collection then go ahead and put it in an array or collection. I'm quite sure that you've created objects and populated arrays and collections before.
If you want to handle events then you have two options:
If you've created the object in the designer then you can include it in the Handles clause of a method. The IDE will create the method and include the Handles clause for you. You can include as many objects and events in a single Handles clause as you like, as long as the event signature matches the signature of the method.
If you've created the object in code then you would use the AddHandler statement to connect the event to the method that will handle it. You need to write the method at design time and then you can use AddHandler to connect it to as many objects and events as you like, as long as the event signature matches the signature of the method.
In either case, if you need to get a reference to the object that raised the event within the method you use the 'sender' argument, which is ALWAYS a reference to the object that raised the event.
-
Nov 23rd, 2006, 06:50 PM
#3
Thread Starter
Frenzied Member
Re: Need object array or objects in a collection with events
OK thanks,
So lets say I have two user controls in the tray. Both of them have the events coded into the form. I want to be able to create a new instance of one of the controls each time one of my events fires which happens to be the connection request.
So i do it like this
VB Code:
Dim sckClient as Winsock = New Winsock
Which is fine, but what happens next time it fires? It might screw up because of the same object name, is there some syntax I should use to stick a number on the end of the object name?
-
Nov 23rd, 2006, 06:59 PM
#4
Thread Starter
Frenzied Member
Re: Need object array or objects in a collection with events
Okay after playing around I'm getting somewhere, the above code would be fine, if i declare my array as a an array of winsocks, then i can just create a winsock at runtime called sckClient, then stick it in the array, I can there just reference it using Array(0).method I think
-
Nov 23rd, 2006, 07:08 PM
#5
Thread Starter
Frenzied Member
Re: Need object array or objects in a collection with events
Im unsure about the addhandler function, is this the best way to add the events to the new object in the array?
VB Code:
AddHandler Things(0).Connected, AddressOf wsck_Connected
AddHandler Things(0).Disconnected, AddressOf wsck_Disconnected
AddHandler Things(0).DataArrival, AddressOf wsck_DataArrival
or would you add the handers before it goes in the array, or am i talking cr*p? lol
Appreciate your advice mate
-
Nov 23rd, 2006, 07:26 PM
#6
Re: Need object array or objects in a collection with events
It doesn't matter... You can create the object, add handlers then put it in the array or create the object, put it in the array then add handlers to it.... Both will give you the same end result. Performance wise, I don't think there's any noticeable difference either.
-
Nov 23rd, 2006, 07:43 PM
#7
Thread Starter
Frenzied Member
Re: Need object array or objects in a collection with events
Right ok I'm getting there I think, thanks stanav and jmcilhinney, I won't mark as resolves just yet as I think there will be a couple more problems tomorrow
-
Nov 23rd, 2006, 08:34 PM
#8
Re: Need object array or objects in a collection with events
You should add the event handlers before adding the object to the array. Given that you already have a direct reference to the object, why would you go to the trouble of accessing it via the array three times? That's three array accesses to get a reference that you already have. It would not make any noticeable difference in practice but it is always good practice to assign an object to a variable if you want to access it multiple times to avoid having to make multiple property accesses. If you had an object in an array and you wanted to add three event handlers it would be considered good practice to do this:
VB Code:
Dim thing As Something = Things(0)
AddHandler thing.Connected, AddressOf wsck_Connected
AddHandler thing.Disconnected, AddressOf wsck_Disconnected
AddHandler thing.DataArrival, AddressOf wsck_DataArrival
In your case you already have a variable that references the object directly so there's no need to declare another.
Now let me clarify and say that it doesn't matter whether you add the object to the array before or after adding the event handlers but you absolutely should use the variable and not the array element as the reference when using the AddHandler statement (note statement, not function).
Also, if you are intending to add and remove objects at run time then it's a good idea to use a collection rather than an array.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|