473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Modify Hash Table Value

bs
I have a hashtable that contains structures. Is it
possible to modify a value of one of these structures.

It only seems to work if the hash contains class objects,
not structures?

Nov 20 '05 #1
2 5007
"bs" <sk*****@optonline.com> schrieb
I have a hashtable that contains structures. Is it
possible to modify a value of one of these structures.

It only seems to work if the hash contains class objects,
not structures?


You are right, it does not work with structures, or more generally speaking,
it does not work with value types. It only works with reference types like
classes.

The reason is that the Hashtable's Item property returns a *copy* of the
internally stored object. The following statement is invalid:

Directcast(myhash("bla"), myStructure).Prop = "test"

When you split the statement above, you get:

dim tmp as mystructure

tmp = Directcast(myhash("bla"), myStructure)
tmp.Prop = "test"

As you can see, only the copy of the structure is changed.

If possible, use a class instead of a structure. If not possible, probably
because the structure has already been defined - maybe externally - you can
write a class as a wrapper for your structure:

Class WrappedStructure
Public Value As MyStructure
End Class

Instead of adding structure objects to the hashtable, create instances of
the class and add them to the hashtable. This only makes sense if the
structure objects haven't been created before, because otherwise you'd
create another copy that is stored in the class object.
--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #2
bs,
To continue with what Armin stated, you can retrieve the structure, modify
and put the structure back.

dim tmp as mystructure

tmp = Directcast(myhash("bla"), myStructure)
tmp.Prop = "test"
myhash("bla") = tmp ' update the hashtable with the modified structure!

However I would recommend using a class, as you have found using a class is
more intuitive.

Hope this helps
Jay

"bs" <sk*****@optonline.com> wrote in message
news:0f****************************@phx.gbl...
I have a hashtable that contains structures. Is it
possible to modify a value of one of these structures.

It only seems to work if the hash contains class objects,
not structures?

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
4
by: Simone Battagliero | last post by:
I wrote a program which inserts and finds elements in an hash table. Each element of the table is a dinamic list, which holds all elements having the same hash value (calculated by an int...
6
by: Chang | last post by:
How to get SHA1 or MD5 of a big file (+5MB - 20GB) as I can't read 20GB into memory. -- Chang.
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
2
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table...
1
by: Ryan Kaskel | last post by:
Hi! I am new to this newsgroup and need help implementing a hash table. This assignment is for school but only concerns one method. Basically we have to write methods like put(), get(),...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.