Link to home
Start Free TrialLog in
Avatar of Arnold Layne
Arnold LayneFlag for United States of America

asked on

Question about Hashtables

My C# knowledge isn't all there yet, but this should be an easy question.

public Hashtable ItemProperties = new System.Collections.Hashtable();

        private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
        {
            ItemProperties = new Hashtable();
            ItemProperties.Add("Title", workflowProperties.Item.Name);
        }

Since I already have this as a field
public Hashtable ItemProperties = new System.Collections.Hashtable();

Why do I need this?

ItemProperties = new Hashtable();

inside the event handler rather than just calling the second line inside the event handler only?
ItemProperties.Add("Title", workflowProperties.Item.Name);

Seems to me that in both cases it is merely being set to an empty hashtable, so there is a concept that I am missing that maybe has to do with what fields really are.

Thanks
SOLUTION
Avatar of plusone3055
plusone3055
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arnold Layne

ASKER

colly92002
You have defined a class level ItemHastable variable (the first line), and in your "onWorkflowActivated" method you are simply setting it to be a "new" empty hashtable.

OK, but it seems to me that
public Hashtable ItemProperties = new System.Collections.Hashtable();

is not just declaring a variable (or field in this case), but it is also initializing it to an empty hashtable.

So what I don't understand is why it has to be recreated within the event handler
ItemProperties = new Hashtable();

It almost seems like the two are doing the same thing.

 plusone3055
Thank you for the article. I can understand how hashtables work, but I don't understand what appears to be a double initialization in the case of this code that I am getting from a course that I am taking. And I don't want to just copy what the course is doing, I need to understand why it is doing that and it doesn't explain, but I assume there is actually a good reason, and that's what I need to find out


AndyAinscow
What that does is throw the previous hashtable away and replace it with a new one.


That's exactly what it seems like to me, and I don't know why it has to do that, but this is code from a course that I am taking, so I assume it's actually correct and there is a reason for this seeming replication. So what would that reason be?
I think I understand it now, and the key is that it has to be reset to an empty hashtable each time that event happens in case there are any previously remaining values laying around, and maybe it is for cancellation purposes or other things that can happen.

Thanks.
the key is that it has to be reset to an empty hashtable each time that event happens in case there are any previously remaining values laying around, and maybe it is for cancellation purposes or other things that can happen.

Yes that is exactly it.  As I say, i'm not convinced you actually need to do this, but if its best practice then its a good idea to follow the pattern.