2

In my project which is kind of a tcp server, i need to keep track of a lot of objects to be used in a global scope. I got addicted towards hashtable and currently im using like 10 hashtables in my project which will store different kinds of objects.

My question is, is this alright? i know that its a pretty huge data strucre but it makes my work very easy, rather than going for some other alternative. For instance i have many groups of textboxes which i need to keep track of. so i put them all in a hashtable and use them as i like.

Is this kind of approach an acceptable and normal way to program when working on a large multithreaded application.

3 Answers 3

7

It's not clear from your example, but it sounds as if you're using global hashmaps to access objects you need (such as textbox in an interface) from any point in your program.

a) Global access like that is not good practice, and even less so with interface objects. Those should be accessed only from the front-end module, your TCP server back-end couldn't care less about those.

b) The correct way of arranging objects in a "functional group" is having a proper Class, not 5-10 hashmaps. Why, in practice? Consider these two approaches (in pythonlike pseduo-code):

# Interface is a hash-map of interface objects, like text-boxes. 'status' is the key
# in the map for one of those.
Interface['status'].setText("New status!")
# vs:
# Interface is a proper object with its own class, that handles the interface. It has
# all our text-boxes "inside" of it.
Interface.updateStatus("New status!")

Now, say you change your mind and want to represent the status as a drawing, instead of a text-box. With the second approach that's easy: you just adjust the GUI accordingly, and change the way the updateStatus method behaves!

With the first approach you now have a mess of hahsmap accesses to a text-box that doesn't exist anymore, spread all over your code.

This is a much more general practice than just hashmaps or global objects -- it's about working with objects that have clearly defined interfaces, and that can change internally without the rest of the program being affected.

Sign up to request clarification or add additional context in comments.

4 Comments

yeah it makes sense, but consider this scenario, i have two sets of textbox controls, what i did is created two hash tables and handled them, but when you talk about classes, c sharp doesnt allow cross class accessing of GUI elements, so how will i be able to access these then
@swordfish Yes it does. But it doesn't allow cross thread accessing of GUI elements.
wow man, when i gave the comment i wasn't that sure of what exactly u meant. I just had a rough idea. But then i tried what you said, i created a manifest class for all the textboxes and created textbox arrays in that class and grouped them. It gave me so much ease in accessing them... thanks man...
@Kristoffer we can do cross thread accessing using a delegate to make sure that its a safe cross threading call
1

The great advantage of using hashtable is that you can store data of any type. Internally HashTable uses Dictionary Type.

If all the data you are storing inside the hashtable is the of the same type i would suggest you to use Generic type.

Dictionary < int, string > or Even List<,> type for better performance.

Comments

0

I see no issue with it as long as your memory consumption it low. Afterwards you should migrate The hash table to disk. Either use a full flown relational database or simply use one of the key value stores. Your choice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.