Monday, October 13, 2008

Dictionary Base----key value pair

content for test.txt :Mike,192.155.12.1,David,192.155.12.2,Bernica,192.155.12.3
*****************************************
using System.Collections;//
namespace DictionaryBaseTest
{
public class IPAddresses : DictionaryBase
{
public IPAddresses()
{ }
public void Add(string name, string ip)
{
base.InnerHashtable.Add(name, ip);
}
public string Item(string name)
{
return base.InnerHashtable[name].ToString();
}
public void Remove(string name)
{
base.InnerHashtable.Remove(name);
}
}

class Program
{
static void Main(string[] args)
{
IPAddresses myIPs = new IPAddresses();
myIPs.Add("Mike", "192.155.12.1");
myIPs.Add("David", "192.155.12.2");
myIPs.Add("Bernica", "192.155.12.3");
Console.WriteLine("There are "+myIPs.Count+" IP addresses");
Console.WriteLine("David's IP is: "+myIPs.Item("David"));
myIPs.Clear();
Console.WriteLine("There are " + myIPs.Count + " IP addresses");
}
}
}
*******************************************
using System.Collections;//DictionaryBase
using System.IO;//StreamReader
namespace DictionaryBaseTest
{
public class IPAddresses : DictionaryBase
{
public IPAddresses()
{
}
public IPAddresses(string txtFile)
{
string line;
string[] words;
StreamReader inFile;
inFile = File.OpenText(txtFile);
while (inFile.Peek() != -1)
{
line = inFile.ReadLine();
words = line.Split(',');
this.InnerHashtable.Add(words[0],words[1]);
this.InnerHashtable.Add(words[2], words[3]);
this.InnerHashtable.Add(words[4], words[5]);
}
inFile.Close();
//foreach (string str in words)
//{
// Console.WriteLine(str);
//}
//Console.WriteLine(line);
}
public void Add(string name, string ip)
{
base.InnerHashtable.Add(name, ip);
}
public string Item(string name)
{
return base.InnerHashtable[name].ToString();
}
public void Remove(string name)
{
base.InnerHashtable.Remove(name);
}
}

class Program
{
static void Main(string[] args)
{
IPAddresses myIPs = new IPAddresses("e:\\fund\\test.txt");

Console.WriteLine("There are " + myIPs.Count + " IP addresses");

Console.WriteLine("Mike's IP is: " + myIPs.Item("Mike"));
Console.WriteLine("David's IP is: " + myIPs.Item("David"));
Console.WriteLine("Bernica's IP is: " + myIPs.Item("Bernica"));
Console.WriteLine();
}
}
}
**********************************************
using System.Collections;//DictionaryBase
using System.IO;//StreamReader
namespace DictionaryBaseTest
{
public class IPAddresses : DictionaryBase
{
public IPAddresses()
{
}
public IPAddresses(string txtFile)
{
string line;
string[] words;
StreamReader inFile;
inFile = File.OpenText(txtFile);
while (inFile.Peek() != -1)
{
line = inFile.ReadLine();
words = line.Split(',');
this.InnerHashtable.Add(words[0],words[1]);
this.InnerHashtable.Add(words[2], words[3]);
this.InnerHashtable.Add(words[4], words[5]);
}
inFile.Close();
//foreach (string str in words)
//{
// Console.WriteLine(str);
//}
//Console.WriteLine(line);
}
public void Add(string name, string ip)
{
base.InnerHashtable.Add(name, ip);
}
public string Item(string name)
{
return base.InnerHashtable[name].ToString();
}
public void Remove(string name)
{
base.InnerHashtable.Remove(name);
}
}

class Program
{
static void Main(string[] args)
{
IPAddresses myIPs = new IPAddresses("e:\\fund\\test.txt");

Console.WriteLine("There are " + myIPs.Count + " IP addresses");

DictionaryEntry[] ips = new DictionaryEntry[myIPs.Count];
myIPs.CopyTo(ips, 0);
for (int i = 0; i <= ips.GetUpperBound(0); i++)
{
//Console.WriteLine(ips[i]);
Console.WriteLine(ips[i].Key);
Console.WriteLine(ips[i].Value);
}
}
}
}
****************************************
static void Main(string[] args)
{
KeyValuePair[] book = new KeyValuePair[10];
book[0] = new KeyValuePair("Mike",99);
book[1] = new KeyValuePair("David", 88);
book[2] = new KeyValuePair("Bernica", 77);
for (int i = 0; i < book.GetUpperBound(0); i++)
{
if (book[i].Value != 0)
{
Console.WriteLine(book[i].Key+" : "+book[i].Value);
}
}
Console.WriteLine();
}
******************************************
static void Main(string[] args)
{
SortedList myIPs = new SortedList();

myIPs.Add("Mike", "192.155.12.1");
myIPs.Add("David", "192.155.12.2");
myIPs.Add("Hernica", "192.155.12.3");
for (int i = 0; i < myIPs.Count; i++)
{
Console.WriteLine("Name: "+myIPs.GetKey(i)+" "+"IP: "+myIPs.GetByIndex(i));
}
}

No comments: