Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > What is wrong here? Arrays!!!
 

What is wrong here? Arrays!!!

I have attempted to create a control array using this method:

Dim WinsockArray As New List(Of AxMSWinsockLib.AxWinsock)
Dim WnSck_Numb As Integer

Sub Create_WinSock()

Dim Winsock_ As New AxMSWinsockLib.AxWinsock

Increase_Array()

WinsockArray.Add(Winsock_)

WinsockArray.Item(WinsockArray.Count).Tag = WnSck_Numb

End Sub


Public Sub Increase_Array()

WnSck_Numb = WnSck_Numb + 1

End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

MsgBox(WinsockArray.Item(1).Tag)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

MsgBox(WinsockArray.Count)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Create_WinSock()
Create_WinSock()
Create_WinSock()

End Sub

When i click Button1 there's apparently only 1 Winsock created, even though i create 3 (using New each time the sub is called). I get a script out of range error when I click Button2, because there is only Index 0 (because it's only created 1 object). I've just moved on from VB 6, this is all new. Can you please tell me what I'm doing wrong?


Slyke  Tuesday, June 13, 2006 5:13 PM

One Item at least is the following line

WinsockArray.Item(WinsockArray.Count).Tag = WnSck_Numb

This needs to be changed to

WinsockArray.Item(WinsockArray.Count-1).Tag = WnSck_Numb

To update the newly added item.

spotty  Tuesday, June 13, 2006 5:36 PM

One Item at least is the following line

WinsockArray.Item(WinsockArray.Count).Tag = WnSck_Numb

This needs to be changed to

WinsockArray.Item(WinsockArray.Count-1).Tag = WnSck_Numb

To update the newly added item.

spotty  Tuesday, June 13, 2006 5:36 PM
That fixed everything up! It now creates 3 items and i can read the tag of them all. Thank you.
Slyke  Tuesday, June 13, 2006 5:39 PM
Spotty is correct, the array is zero-based. You don't need to manually maintain a count of the items as the list has a .Count property that returns the number of items in the collection.
Richard_Wolf  Tuesday, June 13, 2006 5:42 PM
I manually maintain it because i have another array that's a boolean. I use this array to keep a checkup on wether or not a Winsock is connected, (incase i want it to think it's connected if i want to use that paticular winsock for something else). This just helps keep count of all the Winsocks i currently use. If one disconnects, then a new connection will take it's place, or it will add 1 more to the array and to that integer. It just makes it easier for me .
Slyke  Tuesday, June 13, 2006 5:48 PM
Understood.  You might consider making a class that contains the winsock and any associated data you need to maintain then have an array of the classes in your main form rather than the winsock itself.
Richard_Wolf  Tuesday, June 13, 2006 7:30 PM

You can use google to search for other answers

Custom Search

More Threads

• IPAddress object
• splash screen while frmain loading
• Avoiding default instances when using Application Framework
• stopping main form from loading
• Adding Directory/Files to Treeview
• Xml Accessing specefic node
• Visual Basic 6 GridProps
• console version of "Session"
• LINQ XML - Writing output issues with stringwriter and xmlwriter
• Add Data in a Datagridview from other form