473,698 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HashTable gone nuts...

Q
Hi,

I'm using a hashtable to store away a bunch of numbers from my array:

The Hashtable should look like:
Key Value
1 01234
2 01235
3 01245
....
10 23456
I use a module to keep my Hashtable "alive" all times/during the whole
runtime:

Module ModGlobVariable n
Public lfdNrHash As Integer = 1
Public myHashTable As New Hashtable
End Module

I do have a subroutine (very simple)that I call each time a value in my
array has changed:

Private Sub FillHashTable(B yVal Wert)

myHashTable.Add (lfdNrHash, Wert)
lfdNrHash = lfdNrHash + 1

End Sub

Everything works fine, no error is raised, but if I debug step-by.step I
see the following result:

Key Value
1 23456
2 23456
3 23456
...
10 23456

Befor there was correct data in each HashTable Key, but in the end all
data is overwritten with the last value of my array !
Meaning each "myHashTable.Ad d(lfdNrHash, Wert)" deletes all old
hashtable values (not the keys) and replaces them with the new array
value!

Why does "myHashTable.ad d" do that?
Each time a new value with a new key is written, the old precursor value
is overwritten.... .

Pls help.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #1
10 2091
"Q" <MI**@gmx.net > schrieb
Hi,

I'm using a hashtable to store away a bunch of numbers from my
array:

The Hashtable should look like:
Key Value
1 01234
2 01235
3 01245
...
10 23456
I use a module to keep my Hashtable "alive" all times/during the
whole runtime:

Module ModGlobVariable n
Public lfdNrHash As Integer = 1
Public myHashTable As New Hashtable
End Module

I do have a subroutine (very simple)that I call each time a value in
my array has changed:

Private Sub FillHashTable(B yVal Wert)

myHashTable.Add (lfdNrHash, Wert)
lfdNrHash = lfdNrHash + 1

End Sub

Everything works fine, no error is raised, but if I debug
step-by.step I see the following result:

Key Value
1 23456
2 23456
3 23456
..
10 23456

Befor there was correct data in each HashTable Key, but in the end
all data is overwritten with the last value of my array !
Meaning each "myHashTable.Ad d(lfdNrHash, Wert)" deletes all old
hashtable values (not the keys) and replaces them with the new
array value!

Why does "myHashTable.ad d" do that?
Each time a new value with a new key is written, the old precursor
value is overwritten.... .

Pls help.

What is the type of the objects added to the Hashtable? You dropped the data
type of the parameter of sub FillHashTable. I guess it's not Integer, is it?
If it's a reference type, maybe you don't create new instances but change
the value of a property of one single instance that you add multiple times.
Just a guess.
Armin

Nov 21 '05 #2
Hi,

In addition to Armin's comments. I tried this and it worked as
expected.
FillHashTable(" Test")
FillHashTable(C olor.Red)
FillHashTable(B rushes.Green)
FillHashTable(M e.Controls)

For Each de As DictionaryEntry In myHashTable
Trace.WriteLine (String.Format( "{0} {1}", de.Key, de.Value))
Next
And get the follow output

4 System.Windows. Forms.Form+Cont rolCollection
3 System.Drawing. SolidBrush
2 Color [Red]
1 Test
Ken
--------------------------
"Q" <MI**@gmx.net > wrote in message
news:ur******** ******@tk2msftn gp13.phx.gbl...
Hi,

I'm using a hashtable to store away a bunch of numbers from my array:

The Hashtable should look like:
Key Value
1 01234
2 01235
3 01245
...
10 23456
I use a module to keep my Hashtable "alive" all times/during the whole
runtime:

Module ModGlobVariable n
Public lfdNrHash As Integer = 1
Public myHashTable As New Hashtable
End Module

I do have a subroutine (very simple)that I call each time a value in my
array has changed:

Private Sub FillHashTable(B yVal Wert)

myHashTable.Add (lfdNrHash, Wert)
lfdNrHash = lfdNrHash + 1

End Sub

Everything works fine, no error is raised, but if I debug step-by.step I
see the following result:

Key Value
1 23456
2 23456
3 23456
..
10 23456

Befor there was correct data in each HashTable Key, but in the end all
data is overwritten with the last value of my array !
Meaning each "myHashTable.Ad d(lfdNrHash, Wert)" deletes all old
hashtable values (not the keys) and replaces them with the new array
value!

Why does "myHashTable.ad d" do that?
Each time a new value with a new key is written, the old precursor value
is overwritten.... .

Pls help.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 21 '05 #3
Hi
You wert object seems to be same do you recreate it by new keyword
again.Looks like you are adding the same object again and again.

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/

Nov 21 '05 #4
Q
Gosh, thy for all the answers.
I do think your guys are right.
I need to create a new array each time
But how do I do that ?
Somewhere I call my Sub:

Call FillHashTable(F igArray) <--- Any action to be taken here?
Then:
Private Sub FillHashTable(B yRef Wert As Array)
Dim NeuWert As New System.Array <----- won't work

myHashTable.Add (lfdNrHash, NeuWert)
lfdNrHash = lfdNrHash + 1
End sub
----------------

MIK

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #5
Q,

Why are you using a hashtable in the first place, as you show it, you use
just the indexer from the array in the hashtable. A hashtable is to use as a
directory by instance.

Key Value
01234 1
01235 2
01245 3

I hope this helps,

Cor
Nov 21 '05 #6
Q
Well, simple, I "work" with the array (count numbers in a special order)
but after each change in my array the result should be stored internal
in my programm to be processed later on.
A hashtable seemed to me an convenient way to store away hundreds of
numbers.

I think you guys a right. An array is a reference-type object, and seems
to me I keep adding the same array to my hash-table.
But even if I use nother array to store away those numbers wouldn't it
be the same problem?

Do I need to create a new array each time I change a single value in my
array to be able to copy these values into an hashtable/array?

If yes how do I do that?

With System.Array.Cr eateInstance() ???


--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #7
Q,
A hashtable seemed to me an convenient way to store away hundreds of
numbers.

Why it is just as the arraylist a kind of collection that implements
ICollection.

For what you tell is it not the only proper choose when the key is equal to
the index number of an arraylist, than you can use the arraylist itself in
the same case.

See my sample when to use in my previous message.

However feel free to use that hashtable.

Cor

Nov 21 '05 #8
Q
Cor,

frankly I don't care if I use a hashtable or an ArrayList.
But I still have a huge problem with the fact that my array is an
reference-typ object.

I need my original array, and I need always the last value from that
array.
I tried a lot to avoid that my hashtable holds only a reference on the
original array, but frankly I failed.

visual basic code:
Private Sub FillHashTable(B yVal Wert() As Integer)

Dim arr2() As Integer
arr2 = New Integer() {}

arr2 = Wert.Clone

'TestHT = Wert.Clone() <--- Can't clone or concatenate into the
hashtable :-(

myHashTable.Add (lfdNrHash, Wert)
lfdNrHash = lfdNrHash + 1
End Sub

E.g. I tried to create in my sub each time a temporary array (arr2) that
should be destroyed when I leave the sub, right?
But as I need the values from my Original Array (Wert) I used
array.clone and ended up the same.
It's not possible for me to start all over again each time I create a
new array (arr2) because I need these values.

I don't care if I copy into an hashtable or an arraylist, (array won't
work because I don't now the size in the beginning) but everything I
tried either did not work because the conversion from integer() to
integer is not allowed or ended up with the same result.

Isn't there any way from keeping VB just referencing to that original
array?
All I need is a cheap copy....! How can it be that this is so difficult?

Desperate MIKQ

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 21 '05 #9

See if this helps:

Dim MyHashTable As Hashtable = New Hashtable
Dim SaveArray() As Integer

Dim MyArray() As Integer = New Integer(3) {}

'set initial values
MyArray(1) = 1
MyArray(2) = 2
MyArray(3) = 3

'save initial array
SaveArray = New Integer(3) {}
Array.Copy(MyAr ray, 1, SaveArray, 1, 3)

MyHashTable.Add ("initial", SaveArray)

'make changes
MyArray(2) = 200

'save changed array
SaveArray = New Integer(3) {}
Array.Copy(MyAr ray, 1, SaveArray, 1, 3)

MyHashTable.Add ("middle", SaveArray)

'make more changes
MyArray(3) = -8

'save final array
SaveArray = New Integer(3) {}
Array.Copy(MyAr ray, 1, SaveArray, 1, 3)

MyHashTable.Add ("final", SaveArray)
Q wrote:
Gosh, thy for all the answers.
I do think your guys are right.
I need to create a new array each time
But how do I do that ?
Somewhere I call my Sub:

Call FillHashTable(F igArray) <--- Any action to be taken here?
Then:
Private Sub FillHashTable(B yRef Wert As Array)
Dim NeuWert As New System.Array <----- won't work

myHashTable.Add (lfdNrHash, NeuWert)
lfdNrHash = lfdNrHash + 1
End sub
----------------

MIK

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com


Nov 21 '05 #10

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

Similar topics

5
2819
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would like it to return an XML serialized version of an object.
5
15576
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include adding/modifying/deleting. To solve the synchronization issues I am doing two things: 1. Lock...
33
3312
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are used before adding, removing or iterating the Hashtable. The program runs in a high workload environment. After running a few days, now it suddenly catchs this Exception when inserting a pair of key and object, stacktrace =...
16
696
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
3
9693
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
4
3016
by: Joseph Bergevin | last post by:
Why can't I use an int for a key? Doing so evidently doesn't result in unique IDs being generated. I can convert the array into a delimited string, which works fine, but then I have a good deal of overhead parsing/casting the ints back. I'd obviously use a (jagged)multidimensional array of my Value type instead of a key:value setup, but the int sets aren't tightly grouped, so I'd have a lot (maybe 85%) of empty slots in the array. Still,...
2
3143
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I need to write to it, but now it occurred to me that maybe I could just lock one of the Hashtables inside without locking the entire object, but then I thought maybe some thread could instruct the outside Hashtable to remove an inside Hashtable...
1
1279
by: thermate | last post by:
diapers, wig, knife ... guys i am dying of laughter, someone help me ... Seems like the Neoconish spirit of Bush/Cheney has permeated the whole country thru the cell phones and cause the yanks to go mad ... including the internet spoooook gone mad about bAbe ==== http://www.freep.com/apps/pbcs.dll/article?AID=/20070206/NEWS07/70206009/0/BLOG01
2
3879
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that function. Then i am setting this hash table to hashtable which i am passing as ref. So my question is how scope is mention when i am assigning local
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.