HashTable gone nuts...

Q

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 ModGlobVariablen
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(ByVal 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.Add(lfdNrHash, Wert)" deletes all old
hashtable values (not the keys) and replaces them with the new array
value!

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

Pls help.
 
A

Armin Zingler

Q said:
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 ModGlobVariablen
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(ByVal 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.Add(lfdNrHash, Wert)" deletes all old
hashtable values (not the keys) and replaces them with the new
array value!

Why does "myHashTable.add" 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
 
K

Ken Tucker [MVP]

Hi,

In addition to Armin's comments. I tried this and it worked as
expected.


FillHashTable("Test")
FillHashTable(Color.Red)
FillHashTable(Brushes.Green)
FillHashTable(Me.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+ControlCollection
3 System.Drawing.SolidBrush
2 Color [Red]
1 Test


Ken
 
Q

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(FigArray) <--- Any action to be taken here?


Then:
Private Sub FillHashTable(ByRef Wert As Array)
Dim NeuWert As New System.Array <----- won't work

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

Cor Ligthert [MVP]

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
 
Q

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.CreateInstance() ???
 
C

Cor Ligthert [MVP]

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
 
Q

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(ByVal 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
 
L

Larry Lard

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(MyArray, 1, SaveArray, 1, 3)

MyHashTable.Add("initial", SaveArray)

'make changes
MyArray(2) = 200

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

MyHashTable.Add("middle", SaveArray)

'make more changes
MyArray(3) = -8

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

MyHashTable.Add("final", SaveArray)
 
C

Cor Ligthert [MVP]

Q.

Do you mean this

\\\
Private arr as arraylist
Sub AddWertToArray(byval as integer())
arr.add(wert) 'a little bit overdone function of course
end if
///
\\\
Function GetLastWert() as integer()
return directcast(arr(arr.count-1),integer())
end function
///

I typed it in this message so watch typos or other errors.

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top