Solved

Question about Hashtables

Posted on 2013-11-06
6
320 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 24
Set cookies HttpOnly and Secure 4 62
bulid json format 3 21
Achieve json result 2 35
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

10 Experts available now in Live!

Get 1:1 Help Now