Skip to main content
gamedev.net
🔒 Locked

VB.NET: Create/Send/Interpret Array

Started by Al Capwn 2 replies 2.1k views
Original Post
Al Capwn
Al Capwn
Hello everyone, I am new to this site and this is my first post. I am relatively new to programming and this is the first program Ive ever written with networking support. Please bear with me as I already know I am new, my methods are inefficient, and I might ask stupid questions. What I am trying to accomplish is the population of a listbox on my client to match an array on the server (its like a chatroom where a list on the side contains all the connected peoples' names). I kind of understand what I am supposed to do but I do not know how I am supposed to do it. I do not really understand arrays and I have never worked with them before. *Please give me the simplest solution, as long as it works* I have a client and server application that communicate using system.net.sockets. In my code there is a section of code that executes every time a client connects. What I need to do every time a client connects is store the name that the client sends as the next item in an array, and then broadcast the array from the server to all clients, and then interpret the received array by the client so that it can update the listbox to include the new data. I THINK I understand how to do the sending part. I can use the array.join method to concatenate it into a string, delimited by a chosen character, send the string, and then use the Split() function with the same chosen character to split the string back up into its original values. I THINK I can update the listbox by deleting it, then making a loop that will add each of the split values in order into it. This might not be the best method but I understand it and I hope it will work, correct me if it won't. What I DON'T know how to do is create an array that I can keep adding to as people join. In my Sub Main() section I have a counter section of code that executes on AccpetTCPClient(). From there it receives the clients name and broadcasts a connection message and whatnot... so I assume this is where I would stick their name into the array. So, I guess I could've avoided all of this writing and simply asked, "How do I create an array with no defined size (or would I just reDim the array to 1 more each time), and then how do I add data into that array?" but I like to be difficult apparently. Any and all help is greatly appreciated because I am ready to pull my hair out... Thank you in advance
JamesLewis
JamesLewis
Quote:
"How do I create an array with no defined size (or would I just reDim the array to 1 more each time), and then how do I add data into that array?"


Use a List or some other similar container to store your data in. have a look on the MSDN (here) to find out about lists. If you want to add data to it you can use List.Add(), however, from what you've described you may be better off clearing the list completely (List.Clear()) then using List.AddRange() to add the data returned by the server.

Now, I know this is your first Networked program but the application you have described would fit perfectly (and reletively easily) into the WCF The WCF is very simple to use if you want it to be and there's tons of documentation on the internet on how to use it. You can pass List objects between the server and client and you can also use events to signal all the clients when a new user has logged onto the server.

Hope this helps,

James
Al Capwn
Al Capwn
Ok well I took a slightly different (and ugly) method to this. I tried the list thing but couldn't figure out how to store and send it, I really appreciate the help though. I'm sure your method is much better and more efficient but... what can I say I'm an idiot.

So what I did is create a string to keep my list of members in, with each name seperated by a $, such as
ClientList = ClientList + NewClient + "$"
This then results in a string like
Client1$Client2$Client3$...
which I then broadcast out to all clients with ListUpdate$ infront of it. So my client receives the message as
ListUpdate$Client1$Client2$Client3$...
which I then use the Split() function to seperate the string by the $ character.

NOW what I need to do is figure out how to use loops (another thing my VB teacher left out of the course) to say something sorta like:
Dim NewName As StringStart loop variable at 1NewName = Split(ListUpdateString, "$")(variable)Listbox.Items.Add(NewName)variable + 1Next


I've tried looking at some sample loops and I keep crashing my stupid program getting an array index out of bounds error or something so rather than giving myself grey hair I'd rather just ask for the proper way to write the kind of loop that I need.

Who feels like helping me out now?
Al Capwn
Al Capwn
Alright well I got it working, it was simpler than I thought. After sitting around for a while refreshing the browser to see if anyone replied I decided to take another go at it to see if I could get it working. I had to fudge a few things around but, here it is.
                    ListBox1.Items.Clear()                    Dim i As Integer                    Dim n As Integer                    n = Split(returndata, "$").Length - 2                    For i = 0 To n                        Dim item As String                        item = Split(returndata, "$")(i + 1)                        ListBox1.Items.Add(item)                        readData = i                    Next


I don't know why I had to subtract 2 from the length of n but it works that way. My split index is i + 1 because I lead my list of strings with an identifying string, otherwise it would just be i.

Thank you for your help again James!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.