AuthorMessage
Meka][Meka
Unstopable
Posts: 700

here is a little tutorial on using classes, and other storage members, help u also learn the c# syntax,
The Human Project
--------------------------
ok first we need a class to represent a human
Code:
using System;
namespace House {
   public class Human {
      public string sName;
      public int iAge;
   }
}

now we have a simple human class, we need to create a house in main form, under windows form designer code
Code:
Hashtable House = Hashtable.Synchronized(new Hashtable()); //our house storage

now each human will be put into a new room, if u want more then 1 human in 1 room u need to make another class handler which will store each human, but for now jus 1 human per room
first lets create a human
Code:
      private void Form1_Load(object sender, System.EventArgs e)
      {
         Human hum = new Human(); //we want hum to be a new human
            hum.sName = "Meka][Meka"; //this human is called Meka][Meka
         hum.iAge = 666; //666 years old ^_^
      }

ok now we need to add him to the house
Code:
House.Add("Meka][Meka",hum); //to remember which room he was in we will label the door with his name, Meka][Meka

ok now we have stored a human, lets do an example of adding a function to add a the humans name and age without having to write hum.iage etc, open your human.cs....
Code:
using System;
namespace House
{
   public class Human
   {
      public Human(string tName, int tAge) { //sub new (when class is being added as a new)
         sName = tName;
         iAge = tAge;
      }
      public string sName;
      public int iAge;
   }
}

ok now we can change main function from
Code:

      private void Form1_Load(object sender, System.EventArgs e)
      {
         Human hum = new Human(); //we want hum to be a new human
            hum.sName = "Meka][Meka"; //this human is called Meka][Meka
         hum.iAge = 666; //666 years old ^_^
         House.Add("Meka][Meka",hum); //to remember which room he was in we will label the door with his name, Meka][Meka
      }

to:
Code:

      private void Form1_Load(object sender, System.EventArgs e)
      {
         Human hum = new Human("Meka][Meka",666);
         House.Add("Meka][Meka",hum); //to remember which room he was in we will label the door with his name, Meka][Meka
      }

after we add, we'll make another, change form name to mekas name after adding the second, which will show u how to access the items, then we will remove meka
Code:

      private void Form1_Load(object sender, System.EventArgs e)
      {
         Human hum = new Human("Meka][Meka",666);
         House.Add("Meka][Meka",hum);
         hum = new Human("Ashura",666);
         House.Add("Ashura",hum);
         //change form name to meka
         hum = (Human)House["Meka][Meka"];
         this.Text = hum.sName;
         //now remove meka from collection
         House.Remove(hum.sName);
      }

ok that will do for this example, more to come later ;)
-//Meka][Meka
Meka][Meka
Unstopable
Posts: 700

im back for more ;)
ok first lets improve the human class
Code:
using System;
namespace House
{
   public class Human
   {
      public Human(string tName, int tAge) { //sub new (when class is being added as a new)
         Name = tName;
         Age = tAge;
      }
      private string Name; //now we are private
      private int Age; // "
      public int iAge {
         get { return Age; }
         set { Age = value; }
      }
      public string sName {
         get { return Name; }
         set { Name = value; }
      }
   }
}

now back to main form
Code:

Threading.Thread.Sleep(1000); //sleep for 1 second, meka is at work
House.Add("Meka][Meka",hum); //ok meka is back from work

ok ashura and meka need a bit fun now, want a child :-P lets write a fake function for generating another human
Code:

private hum Mate(string humm,string humf) //for now we only need ther names
{
   //lets create a new name
   string name = humm.Substring(0,3) + humf.Substring(humf.Length-3,3); //first 3 letters of hum male and 3 last of hum female
   hum thum = new Human(name,0);
   return thum;
}

Code:
      private void Form1_Load(object sender, System.EventArgs e)
      {
         Human hum = new Human("Meka][Meka",666);
         House.Add("Meka][Meka",hum);
         hum = new Human("Ashura",666);
         House.Add("Ashura",hum);
         //change form name to meka
         hum = (Human)House["Meka][Meka"];
         this.Text = hum.sName;
         //now remove meka from collection
         House.Remove(hum.sName);
         Threading.Thread.Sleep(1000); //sleep for 1 second, meka is at work
         House.Add("Meka][Meka",hum); //ok meka is back from work
         //time to mate :-P
         hum = Mate("Meka][Meka","Ashura"); //get the child object
         House.Add(hum.sName,hum); //add the new child to the house :)
      }

at training atm so aint got time todo more got to do bit more work, soon back home, maybe will do some more, hopefuly when im finished i will cover using of many methods and types of objects as possible
-//Meka][Meka
Rox
n00b
Posts: 11

Cool Tutorial 
But i have one question how save to file and load data from Hashtable ?
Devil][Devil
Clone
Posts: 2

It's awsome ;)
Nice job Meka. You're teh master
The Architect
n00b
Posts: 14

I did not know you can add data to a namespace. However, I define a custom NameObjectCollectionBase for a collection. However, so far I cannot show an example since there is a restriction in the amount of characters I can post.
Meka][Meka
Unstopable
Posts: 700

should of fixed the limit for now....
The Architect
n00b
Posts: 14

Thanks. The NameObjectBaseCollection and UserData class I define is as follows, to give a general sense of custom collections in C#:
Code:
using System;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
public class CollectionObj : NameObjectCollectionBase
{
    public CollectionObj()
    {
    }
    // Add elements from IDictionary
    public CollectionObj(IDictionary d, Boolean bReadOnly)
    {
        foreach (DictionaryEntry de in d)
        {
            this.BaseAdd((String)de.Key, de.Value);
        }
        this.IsReadOnly = bReadOnly;
    }
    // get or set value from key
    public UserData this[String key]
    {
        get
        {
            return (UserData)Convert.ChangeType(this.BaseGet(key), typeof(UserData));
        }
        set
        {
            this.BaseSet(key, value);
        }
    }
    //return all keys
    public String[] AllKeys
    {
        get
        {
            return (this.BaseGetAllKeys());
        }
    }
    //return all objects in collection
    public Array AllValues
    {
        get
        {
            return (this.BaseGetAllValues());
        }
    }
    //return a string array of values
    public String[] AllStringValues
    {
        get
        {
            return ((String[])this.BaseGetAllValues(typeof(string)));
        }
    }
    //does the base class have keys?
    public Boolean HasKeys
    {
        get
        {
            return (this.BaseHasKeys());
        }
    }
    //not really needed, but Add is easier to remember as a method than BaseAdd
    public void Add(String key, Object value)
    {
        this.BaseAdd(key, value);
    }
    //read comment line above
    public void Remove(String key)
    {
        this.BaseRemove(key);
    }
    //clear the entire collection of items
    public void Clear()
    {
        this.BaseClear();
    }
}
public class UserData
{
    public Socket Sock;
    public string sNick;
    public string sIP;
    public string sMyInfoString;
    public string sUserId;
    public UserData(Socket Socket, string nick, string ip, string myinfo, string userid)
    {
        sUserId = userid;
        Sock = Socket;
        sNick = nick;
        sIP = ip;
        sMyInfoString = myinfo;
    }
}