Solved

Question about Hashtables

Posted on 2013-11-06
6
338 Views
Last Modified: 2013-11-06
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
0
Comment
Question by:BobHavertyComh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 100 total points
ID: 39627202
I think this article will give a good explanation :)

http://www.dotnetperls.com/hashtable
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
ID: 39627215
What that does is throw the previous hashtable away and replace it with a new one.
0
 
LVL 15

Accepted Solution

by:
colly92002 earned 300 total points
ID: 39627221
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.

So, each time your workflow starts, you are resetting your hastable to be empty, so you can start again, as your workflow starts.

Whether or not this is actually needed I'm not sure, since I think your "onWorkflowActivated" can only be called once without cancelling and restarting your workflow.   However workflows are complicated things so I'm probably wrong :)
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 9

Author Comment

by:BobHavertyComh
ID: 39627255
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?
0
 
LVL 9

Author Closing Comment

by:BobHavertyComh
ID: 39627269
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.
0
 
LVL 15

Expert Comment

by:colly92002
ID: 39627599
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.
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question