Hi,
can anyone tell me how to compare to keys of a hashtable.I have a situation where my hashtable have same keys.I know it cant be the same but i am inserting a hashtable of dictionary which do have same keys.Thanks
sdhawan -2 Junior Poster
apegram 302 LINQ! Team Colleague
If you could describe your problem a bit more. But in general, if you are adding reference types as keys, the comparison is going to be made between references rather than the unique nature of the information being referenced. For example, if I have a Hashtable where I am storing keys as objects of type Foo
class Foo
{
public int Bar { get; set; }
}
There is nothing stopping me from doing this
Foo foo1 = new Foo() { Bar = 1 };
Foo foo2 = new Foo() { Bar = 1 };
Hashtable hashtable = new Hashtable();
hashtable.Add(foo1, "some value");
hashtable.Add(foo2, "some other value");
foo1 and foo2 hold the same information, but they are not referencing the same object in memory and as a result they each qualify to be a key in the Hashtable. If I have access to the source code for Foo (and I do), I can remedy this by overriding Equals and GetHashCode so that a comparison can be made by what the objects hold rather than the objects themselves.
class Foo
{
public int Bar { get; set; }
public override bool Equals(object obj)
{
return this.Bar.Equals(((Foo)obj).Bar);
}
public override int GetHashCode()
{
return this.Bar.GetHashCode();
}
}
At this point, if I try to add both of my Foo objects to the Hashtable, I will get an exception on foo2 because it will match the foo1 key.
However, I may not have the ability to modify the source of Foo. In that case, I can create an implementation of IEqualityComparer that can then be passed into the Hashtable via a constructor overload.
class FooComparer : IEqualityComparer
{
public bool Equals(object x, object y)
{
return ((Foo)x).Bar.Equals(((Foo)y).Bar);
}
public int GetHashCode(object obj)
{
return ((Foo)obj).Bar.GetHashCode();
}
}
And in the Hashtable instantiation
Hashtable hashtable = new Hashtable(new FooComparer());
// can add foo1 successfully, foo2 will throw exception
Of course, if even this option doesn't make sense, you are left with comparing the contents of the candidate key objects yourself before adding those objects to the Hashtable.
sdhawan -2 Junior Poster
Thanks this helpeded alot .I have one more question .If i am declaring my column as nvarchar in sql and my string is longer than 4000 characters , how can i include the rest of it to the next row.
Thanks
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
DataSet CustomersDataSet = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommandBuilder cmdBuilder;
string myString;
DataSet ds = new DataSet();
//cn.ConnectionString = "Server=server;Database=RTLUser;UID=sa;";
cn.Open();
GermanDict.DictList ins = new GermanDict.DictList();
Hashtable aaa = ins.AllWords;
StringBuilder insertCommand = new StringBuilder();
IDictionaryEnumerator en = aaa.GetEnumerator();
if (aaa.Count > 0)
{
while (en.MoveNext())
{
ArrayList al = (ArrayList)en.Value;
string allData = "";
for (int i = 0; i< al.Count; i++)
{
allData += (string)al[i] + "\r\n";
}
allData = allData.Replace("'", "''");
myString = @"INSERT INTO germanTbl Values('" + en.Key + "','" + allData + "')";
SqlCommand myCmd = new SqlCommand(myString, cn);
myCmd.ExecuteNonQuery();
}
cn.Close();
}
sdhawan -2 Junior Poster
or if you can tell me how to stop the string there and continue with the next value.
Thanks
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.