Domov > C#, Code, Programs, Windows > C# Example of data managment, ArrayList, List

C# Example of data managment, ArrayList, List

junij 27, 2010 Komentiraj Go to comments

Language: C#

This code doesn’t do anything usefull, it is only an example how can be used some of function as ArrayList, List<>, class and some other, as i has start programming in C# i write some Irc client, and this the code i will be using for managing users and channels.
So I write this code becouse i wan’t see and show you how can you write and irc chat client mybe will be usefull to you, for me is it. :)

Programski jezik: C#

No tuki je en primer kako lahko shranjujemo uporabnike kanale in sporočila, če programiramo irc client, celotno stvar se verjetno, da sprogramirat boljše in slabše, in še na 100 drugih načino. No mogoče kdo kaj uporabnega najde v tej kodi. :)

/*
 * User: upd
 * Date: 21.6.2010
 * Time: 16:07
 *
 * This code doesn't do anything usefull, it is only an example how can be used some
* of function as ArrayList, List<>, class and some other, as i has start programming
* in C# i write some Irc client, and this the code i will be using for managing
* users and channels...
* So I write this code becouse i wan't see and show you how can you write and irc chat client
* mybe will be usefull to you, for me is it.
 */
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text;

 namespace Array
 {
 // and now this will be my example that i need it for irc...
 //    List<Person> person = new List<Person>();

 class Channel
 {
 public List<User> users = new List<User>();
 public string Name;
 public ArrayList msg = new ArrayList();

 public Channel(string text)
 {
 Name = text;
 }

 public void AddMsg(string text)
 {
 msg.Add(text);
 }
 }

 class User
 {
 public string Name;

 public User(string text)
 {
 Name = text;
 }
 }

 class Program
 {
 public static void Main(string[] args)
 {
 List<Channel> chan = new List<Channel>();

 Console.WriteLine("Hello World!");

 // First we create channels...
 Console.WriteLine("Creating channel #upd");
 chan.Add(new Channel("#upd"));

 Console.WriteLine("Creating channel #chatting");
 chan.Add(new Channel("#chatting"));

 Console.WriteLine("Creating channel #coders");
 chan.Add(new Channel("#coders"));

 // Now we will add users on channels
 Console.WriteLine("Adding users on channels");
 chan[0].users.Add(new User("upd"));
 chan[0].users.Add(new User("lolek"));

 chan[1].users.Add(new User("chatter 1"));
 chan[1].users.Add(new User("chatter 2"));
 chan[1].users.Add(new User("chatter 3"));
 chan[1].users.Add(new User("chatter 4"));

 chan[2].users.Add(new User("Coder one"));
 chan[2].users.Add(new User("Coder two"));
 chan[2].users.Add(new User("Coder three"));
 chan[2].users.Add(new User("Coder four"));
 chan[2].users.Add(new User("coder five"));

 //            Console.WriteLine("\n");
 Console.WriteLine("Printing all users on all channels\n");

 for ( int i = 0 ; i < chan.Count ; i++ )
 {
 Console.WriteLine("Chan " + chan[i].Name + " has users:");
 for ( int l  = 0 ; l < chan[i].users.Count ; l++ )
 {
 Console.Write(" " + chan[i].users[l].Name + ",");
 }
 Console.Write("\n");
 }

 // add lover nickname on channel #chatting
 Console.WriteLine("\nAdding user with nickname Lover on channel #chatting");

 int index = search_for_channel("#chatting", chan);

 if ( index != -1 )
 {
 Console.WriteLine("Channel #chatting has been finded in database");
 chan[index].users.Add(new User("Lover"));
 }

 // add Michael user to channel #bomb and #upd, if channel does not exsist
 // than create it!
 Console.WriteLine("\nAdding user with nickname Michael on channel #bomb and #upd");

 index = search_for_channel("#bomb", chan);

 if ( index != -1 )
 {
 Console.WriteLine("Channel #bomb has been finded in DB");
 chan[index].users.Add(new User("Michael"));
 }
 else
 {
 Console.WriteLine("Channel #bomb hasn't been finded so i will create it and add user Michael");
 chan.Add(new Channel("#bomb"));
 chan[chan.Count-1].users.Add(new User("Michael"));
 }

 // add user Michael to #upd channel
 index = search_for_channel("#upd", chan);

 if ( index != -1 )
 {
 Console.WriteLine("Channel #upd has been finded in DB");
 chan[index].users.Add(new User("Michael"));
 }
 else
 {
 Console.WriteLine("Channel #upd isn't been finded so i will create it and add user Michael");
 chan.Add(new Channel("#upd"));
 chan[chan.Count-1].users.Add(new User("Michael"));
 }

 Console.WriteLine("\nPrinting all users and channels again!\n");
 //            Console.WriteLine("\n");

 for ( int i = 0 ; i < chan.Count ; i++ )
 {
 Console.WriteLine("Chan " + chan[i].Name + " has users:");
 for ( int l  = 0 ; l < chan[i].users.Count ; l++ )
 {
 Console.Write(" " + chan[i].users[l].Name + ",");
 }
 Console.Write("\n");
 }

 // Show one which channel's is user Michael, we will loop trought all channels and all users on each channel
 Console.WriteLine("\n");
 for ( int i = 0 ; i < chan.Count ; i++ )
 {
 for (int l = 0 ; l < chan[i].users.Count ; l++ )
 {
 if ( chan[i].users[l].Name == "Michael" )
 {
 Console.WriteLine("User Michael is on channel " + chan[i].Name);
 break;
 }
 }
 }

 // And now the finnal test which will be message of users
 // When some user say something we need to store message
 // so we can later redisplay it, so we need to add in channel class
 // some dynamic array for string and that will be ArrayList
 // Let's say we recive data.. "Michael #upd :This is some message"
 index = search_for_channel("#upd", chan);

 if ( index != -1 )
 {
 Console.WriteLine("Adding message to #upd channel");
 chan[index].AddMsg("This is some message...");
 chan[index].AddMsg("and here is the continue of the message.\n");
 chan[index].AddMsg("Oh i forget something to say, this is nice example.");
 }

 index = search_for_channel("#bomb", chan);

 if ( index != -1 )
 {
 string buf = PrintMsg(index, chan);

 if ( buf != null )
 {
 Console.WriteLine("Message of #bomb channel is " + buf);

 }
 }

 index = search_for_channel("#upd", chan);

 if ( index != -1 )
 {
 string buf = PrintMsg(index, chan);

 if ( buf != null )
 {
 Console.WriteLine("Message of #upd channel is " + buf);

 }
 }
 Console.ReadKey(true);
 }
 // end.....of main....

 private static int search_for_channel(string text, List<Channel> chtmp)
 {
 for ( int i = 0 ; i <  chtmp.Count ; i++)
 {
 if ( chtmp[i].Name == text )
 return i;
 }
 return -1;
 }

 private static string PrintMsg(int index, List<Channel> chtmp)
 {
 string buf = "";

 if ( chtmp[index].msg.Count <= 0 )
 {
 return null;
 }

 for ( int i = 0 ; i < chtmp[index].msg.Count ; i++ )
 {
 buf = buf + chtmp[index].msg[i];
 }
 return buf;
 }
 }
 }

Categories: C#, Code, Programs, Windows
  1. Zaenkrat še ni komentarjev.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.