Monday 17 December 2012

c sharp programming


Fundamental DictionaryBase Class
Methods and Properties
When working with a dictionary object, there are several operations you want
to perform. At aminimum, you need an Addmethod to add new data, an Item
method to retrieve a value, a Remove method to remove a key–value pair, and
a Clear method to clear the data structure of all data.
Let’s begin the discussion of implementing a dictionary by looking at a
simple example class. The following code shows the implementation of a
class that stores names and IP addresses:


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);
}
}

As you can see, these methods were very easy to build. The ?rst method
implemented is the constructor. This is a simple method that does nothing
but call the default constructor for the base class. The Add method takes a
name/IP address pair as arguments and passes them to the Add method of the
InnerHashTable object, which is instantiated in the base class.
The Item method is used to retrieve a value given a speci?c key. The key is
passed to the corresponding Item method of the InnerHashTable object. The
value that is stored with the associated key in the inner hash table is returned.
Finally, the Remove method receives a key as an argument and passes
the argument to the associated Remove method of the inner hash table. The
method then removes both the key and its associated value from the hash
table.

There are two methods we can use without implementing them: Count
and Clear. The Count method returns the number of DictionaryEntry objects
stored in the inner hash table, whereas Clear removes all the DictionaryEntry
objects from the inner hash table.
Let’s look at a program that utilizes these methods:

class chapter9 {
static void Main() {
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 address: " +
myIPs.Item("David"));
myIPs.Clear();

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

The output from this program is in the image below


One modi?cation we might want to make to the class is to overload the
constructor so that we can load data into a dictionary from a ?le. Here’s the
code for the new constructor, which you can just add into the IPAddresses
class de?nition:
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]);
}
inFile.Close();
}

Now here’s a new program to test the constructor:
class chapter9 {
static void Main() {
for(inti=0;i<4; i++)
Console.WriteLine();

IPAddresses myIPs = _
new IPAddresses("c:\\data\\ips.txt");
Console.WriteLine("There are {0} IP addresses",
myIPs.Count);
Console.WriteLine("David's IP address: " +
myIPs.Item("David"));
Console.WriteLine("Bernica's IP address: " +
myIPs.Item("Bernica"));
Console.WriteLine("Mike's IP address: " +
myIPs.Item("Mike"));
}
}


No comments:

Post a Comment