T
TonyJ
Hello!
Below is simple working program(main is missing) that works but I wonder
about a few things and that has to do with the IndexOf that is used.
IndexOf calls the overrided Equals below.
My first question is: Is it really necessary to have the GetHashCode it
just calls the name.GetHashCode() so what does this do.
My second question is that a type check is done in Equals to check that
"other is Name" is that a good thing to have or can it be removed.
My third question is if it's better to rewrite the overridden Equals in this
way instead or is it just a matter of taste.
public override bool Equals(object other)
{
return (other is Name) && ((Name)other).name == name;
// return (other is Name) && Equals((Name)other);
}
namespace Indexers
{
struct Name
{
private string name;
public Name(string text)
{
this.name = text;
}
public string Text
{
get { return this.name; }
}
public override int GetHashCode()
{
return name.GetHashCode();
}
public override bool Equals(object other)
{
return (other is Name) && Equals((Name)other);
}
public bool Equals(Name other)
{
return this.name == other.name;
}
}
}
namespace Indexers
{
using System;
sealed class PhoneBook
{
private int used;
private Name[] names;
private PhoneNumber[] phoneNumbers;
public PhoneBook()
{
int initialSize = 0;
this.used = 0;
this.names = new Name[initialSize];
this.phoneNumbers = new PhoneNumber[initialSize];
}
public void Add(Name name, PhoneNumber number)
{
enlargeIfFull();
this.names[used] = name;
this.phoneNumbers[used] = number;
this.used++;
}
public Name this[PhoneNumber number]
{
get
{
int i = Array.IndexOf(this.phoneNumbers, number);
if (i != -1)
return this.names;
else
return new Name();
}
}
public PhoneNumber this[Name name]
{
get
{
int i = Array.IndexOf(this.names, name);
if (i != -1)
return this.phoneNumbers;
else
return new PhoneNumber();
}
}
private void enlargeIfFull()
{
if (this.used == this.names.Length)
{
int bigger = used + 16;
Name[] moreNames = new Name[bigger];
this.names.CopyTo(moreNames, 0);
PhoneNumber[] morePhoneNumbers = new PhoneNumber[bigger];
this.phoneNumbers.CopyTo(morePhoneNumbers, 0);
this.names = moreNames;
this.phoneNumbers = morePhoneNumbers;
}
}
}
}
namespace Indexers
{
struct PhoneNumber
{
private string number;
public PhoneNumber(string text)
{
this.number = text;
}
public string Text
{
get { return this.number; }
}
public override int GetHashCode()
{
return this.number.GetHashCode();
}
public override bool Equals(object other)
{
return (other is PhoneNumber) && Equals((PhoneNumber)other);
}
public bool Equals(PhoneNumber other)
{
return this.number == other.number;
}
}
}
namespace Indexers
{
partial class Form1 : Form
{
private PhoneBook phoneBook = new PhoneBook();
public Form1()
{
InitializeComponent();
}
private void findPhone_Click(object sender, System.EventArgs e)
{
string text = name.Text;
if (text != "")
{
phoneNumber.Text = phoneBook[new Name(text)].Text;
}
}
private void findName_Click(object sender, System.EventArgs e)
{
string text = phoneNumber.Text;
if (text != "")
{
name.Text = phoneBook[new PhoneNumber(text)].Text;
}
}
private void add_Click(object sender, System.EventArgs e)
{
if (name.Text != "" && phoneNumber.Text != "")
{
phoneBook.Add(new Name(name.Text),
new PhoneNumber(phoneNumber.Text));
name.Text = "";
phoneNumber.Text = "";
}
}
}
}
//Tony
Below is simple working program(main is missing) that works but I wonder
about a few things and that has to do with the IndexOf that is used.
IndexOf calls the overrided Equals below.
My first question is: Is it really necessary to have the GetHashCode it
just calls the name.GetHashCode() so what does this do.
My second question is that a type check is done in Equals to check that
"other is Name" is that a good thing to have or can it be removed.
My third question is if it's better to rewrite the overridden Equals in this
way instead or is it just a matter of taste.
public override bool Equals(object other)
{
return (other is Name) && ((Name)other).name == name;
// return (other is Name) && Equals((Name)other);
}
namespace Indexers
{
struct Name
{
private string name;
public Name(string text)
{
this.name = text;
}
public string Text
{
get { return this.name; }
}
public override int GetHashCode()
{
return name.GetHashCode();
}
public override bool Equals(object other)
{
return (other is Name) && Equals((Name)other);
}
public bool Equals(Name other)
{
return this.name == other.name;
}
}
}
namespace Indexers
{
using System;
sealed class PhoneBook
{
private int used;
private Name[] names;
private PhoneNumber[] phoneNumbers;
public PhoneBook()
{
int initialSize = 0;
this.used = 0;
this.names = new Name[initialSize];
this.phoneNumbers = new PhoneNumber[initialSize];
}
public void Add(Name name, PhoneNumber number)
{
enlargeIfFull();
this.names[used] = name;
this.phoneNumbers[used] = number;
this.used++;
}
public Name this[PhoneNumber number]
{
get
{
int i = Array.IndexOf(this.phoneNumbers, number);
if (i != -1)
return this.names;
else
return new Name();
}
}
public PhoneNumber this[Name name]
{
get
{
int i = Array.IndexOf(this.names, name);
if (i != -1)
return this.phoneNumbers;
else
return new PhoneNumber();
}
}
private void enlargeIfFull()
{
if (this.used == this.names.Length)
{
int bigger = used + 16;
Name[] moreNames = new Name[bigger];
this.names.CopyTo(moreNames, 0);
PhoneNumber[] morePhoneNumbers = new PhoneNumber[bigger];
this.phoneNumbers.CopyTo(morePhoneNumbers, 0);
this.names = moreNames;
this.phoneNumbers = morePhoneNumbers;
}
}
}
}
namespace Indexers
{
struct PhoneNumber
{
private string number;
public PhoneNumber(string text)
{
this.number = text;
}
public string Text
{
get { return this.number; }
}
public override int GetHashCode()
{
return this.number.GetHashCode();
}
public override bool Equals(object other)
{
return (other is PhoneNumber) && Equals((PhoneNumber)other);
}
public bool Equals(PhoneNumber other)
{
return this.number == other.number;
}
}
}
namespace Indexers
{
partial class Form1 : Form
{
private PhoneBook phoneBook = new PhoneBook();
public Form1()
{
InitializeComponent();
}
private void findPhone_Click(object sender, System.EventArgs e)
{
string text = name.Text;
if (text != "")
{
phoneNumber.Text = phoneBook[new Name(text)].Text;
}
}
private void findName_Click(object sender, System.EventArgs e)
{
string text = phoneNumber.Text;
if (text != "")
{
name.Text = phoneBook[new PhoneNumber(text)].Text;
}
}
private void add_Click(object sender, System.EventArgs e)
{
if (name.Text != "" && phoneNumber.Text != "")
{
phoneBook.Add(new Name(name.Text),
new PhoneNumber(phoneNumber.Text));
name.Text = "";
phoneNumber.Text = "";
}
}
}
}
//Tony