Solved

Question about Hashtables

Posted on 2013-11-06
6
324 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now