What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » C# anyone?

C# anyone?
Author: Message:
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. C# anyone?
I'm starting to learn C# (by my own, so far, with a teacher soon).
I'd like to create a linked list able to store 2 variables.

I'm working with TCP connections, and one of these variables is obviously the client socket, which is the only one I'm storing right now:
C# code:
private LinkedList<Socket> list = new LinkedList<Socket>();
I also want to store a string, representing the client name, for example.
Is that possible?

I thought of creating a new structure ("transcoding" what I learned on C), but I'd need to use pointers, and I read somewhere C# has no pointers...

Edit: reading the reference on MSDN I found a Dictionary. Do you think replacing the linked list by the dictionary would work?
The methods I need are Add, RemoveFirst, Cound and I also use:
C# code:
foreach(Socket s in list) //cmd

I'm afraid it's not possible due to this:
C# code:
while (list.Count != 0) list.RemoveFirst(); // this is using LinkedList
I don't think I can remove one element without telling which on, right?

This post was edited on 06-25-2011 at 01:45 AM by Chancer.
06-25-2011 01:32 AM
Profile E-Mail PM Find Quote Report
Mnjul
forum super mod
******

Avatar
plz wub me

Posts: 5396
Reputation: 58
– / Other / Flag
Joined: Nov 2002
Status: Away
RE: C# anyone?
You can't do a RemoveFirst on a Dictionary.

Why would you need pointers?

Anyway, http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx would suffice (just create a List<KVP<String, Socket>>, or swap String & Socket if you want the latter to be the key.).

This post was edited on 06-25-2011 at 09:52 AM by Mnjul.
06-25-2011 09:43 AM
Profile PM Web Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: C# anyone?
Very interesting. I'll try that later.
Thanks.
06-25-2011 04:39 PM
Profile E-Mail PM Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: C# anyone?
There is no reason why you can't use LinkedList with your own class that holds the Socket and whatever additional item(s) you want.

I think you are misinterpreting that there are "no pointers" in C# to also mean there are no references, which isn't the case.  What that means, in essence, is you can't easily do funky pointer math and you shouldn't count on being able to know how your data types are laid out on the low level.  You may wish to read this.

06-28-2011 04:08 AM
Profile E-Mail PM Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
35 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: C# anyone?
Interesting. Actually, since it's the first time I'm using an object-oriented language (I've "used" Python once, but I don't think that counts), this things of classes, methods, events, they are still a bit messed in my head.
I'm not really worried about it now, because soon I'll have some classes and (hopefully) this mess will be cleared. I just wanted to start coding a little bit using what I know from C and some extra new stuff.

In C, I could do this:
C code:
struct client {
    int socket_descriptor;
    char[10] name;
    struct client *next;
    struct client *prev;
}
next and prev were my problem on C#. It's easy with C pointers and I wanted to know if I could do that on C#. After reading that page I'm still not sure, maybe I'll give it a try later. But I bet using ref will be useful when I effectively start coding. Thanks.

And about my program, turns out that I was using RemoveFirst to clean the whole list, so I was able to use a Dictionary and it's method Clean.
In the end, I have a Chat server and client (like chatting online in the 90's :P), with some limitations, but I really liked the interface (everything I had coded until now was console-apps). Too bad no one is going to use it :(
06-28-2011 07:18 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On