Arnold Layne
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.Hashtab le();
private void onWorkflowActivated1_Invok ed(object sender, ExternalDataEventArgs e)
{
ItemProperties = new Hashtable();
ItemProperties.Add("Title" , workflowProperties.Item.Na me);
}
Since I already have this as a field
public Hashtable ItemProperties = new System.Collections.Hashtab le();
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.Na me);
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
public Hashtable ItemProperties = new System.Collections.Hashtab
private void onWorkflowActivated1_Invok
{
ItemProperties = new Hashtable();
ItemProperties.Add("Title"
}
Since I already have this as a field
public Hashtable ItemProperties = new System.Collections.Hashtab
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"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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.
ASKER
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.Hashtab
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?