1

im creating a feature in my app that will re create the CTRL+Z thing. i got few textboxs and i made a table like this:

hashtable textChanges[obj.Name, obj.Text] = new HashTable(50);

im having problem extractthe value from a chossen key. im getting the key afther the keyDown is fired.

in the event im looking for the control with focus, and use his name to extract the last value the he enter the table.

this is the event code:

 private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyData == Keys.Z)
        {
            for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i].Focused)
                {
                    if (this.Controls[i].GetType() == typeof(TextBox))
                    {
                        TextBox obj = (TextBox)this.Controls[i];
                        obj.Text = textChanges[obj.Name]; // <--- compile error
                       //Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)   

                    }
                }
            }
        }
    }

this is how i add keys & value to the HashTable

private void textBox_OnTextChange(object sender, EventArgs e)
    {
        if (sender.GetType() == typeof(TextBox))
        {
            TextBox workingTextBox = (TextBox)sender;
            textChanges.Add(workingTextBox.Name, workingTextBox.Text);
        }

        if (sender.GetType() == typeof(RichTextBox))
        {
            RichTextBox workingRichTextBox = (RichTextBox)sender;
            textChanges.Add(workingRichTextBox.Name, workingRichTextBox.Text);
        }
    }

why do i get missing a cast error?

(sorry for my english)

samy
  • 1,949
  • 6
  • 39
  • 64
  • 6
    Any reason you're still using `HashTable`? Generic collections including `Dictionary<,>` came out 7 years ago, and make life *considerably* nicer... – Jon Skeet Oct 20 '12 at 14:42
  • i thgohot Generics dosnt work on XP. am i worng? – samy Oct 20 '12 at 14:43
  • FYI -- both `TextBox` and `RichTextBox` are also [`TextBoxBase`](http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase(v=vs.100).aspx) and it has properties of `Name` and `Text`, among others. – Austin Salonen Oct 20 '12 at 14:46
  • 2
    By `The CTRL-Z thing`, do you mean an undo feature? `HashTables` don't necessarily maintain order. You might want to use a `Stack` to pop each event in order. – keyboardP Oct 20 '12 at 14:47
  • @AustinSalonen thank u i didnt know about that. i will use it! – samy Oct 20 '12 at 14:48
  • 2
    You can run .net 4.0 on WinXP. Generics were introduced in 2.0. There is little reason to avoid .net 2.0 features. – CodesInChaos Oct 20 '12 at 15:05
  • @samy: You should also move off an obsolete OS (XP) if you possibly can... – Jon Skeet Oct 20 '12 at 18:02

2 Answers2

3

You need to convert it to string. It would be better if you use dictionary. Dictionary is a generic type and you do not need types casting which is required for hashtable. Hashtable stores the object type and you are required to type cast the object back to your desired type.

obj.Text = textChanges[obj.Name].ToString();
Adil
  • 146,340
  • 25
  • 209
  • 204
  • but isnt obj.Name return a string? – samy Oct 20 '12 at 14:41
  • Hash table stores object not the types you better use dictionary, http://stackoverflow.com/questions/301371/why-is-dictionary-preferred-over-hashtable – Adil Oct 20 '12 at 14:43
  • ohh, ok i get it now thx. i will give u right answer when the 10 mintus wair are over. – samy Oct 20 '12 at 14:45
2

Yes, you need type casting.

But consider refactoring your code. User generic dictionary instead of hashtable:

Dictionary<string, string> textChanges = new Dictionary<string, string>(50);

And use Linq to retrieve focused textboxes:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyData == Keys.Z)
    {
        foreach (var textBox in Controls.OfType<TextBox>().Where(x => x.Focused))
            textBox.Text = textChanges[textBox.Name];
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459