Solved

Question about Hashtables

Posted on 2013-11-06
6
330 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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