Array

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

I get the number.
Now I must check if this number already was once and if not than store it.

What is the best way to do that?
Create array at the beginning.
When the first number comes, add number to it and when each new number
comes, check if it is in the array already and if not add it or something
similar?

Thank you for your answer,
Simon
 
I forgot to mention:

If you want to have an array at the end, it's easy to convert an ArrayList
to a real array, just use the ToArary function:

Dim myArray() As Integer = al.ToArray(GetType(Integer))

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
you could create a class that has all the info (dont forget the tostring
function) and put that in the arraylist
then you can have as mutch info as you want
 
* "simon said:
Now I must check if this number already was once and if not than store it.

What is the best way to do that?
Create array at the beginning.

I would add the numbers to a Hashtable and check if it already exists.
 
Hi Simon,

as an alternative to EricJ,

You can also create a datatable
Dim dt As New DataTable("Colours")
dt.Columns.Add(New DataColumn("Number", Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Colour", Type.GetType("System.String")))
dim dr as datarow = dt.newrow
dr("Number") = yournumber
dr("Colour")=yourcolour

And then you can go on a little bit the same as with the example of Jan

I hope this helps something

Cor
 
Would you prefer a hashtable over an arraylist is this scenario? Because a
hashtable additionally needs a key object, and my guess this is not used in
this scenario...

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Now I have 3 values: number,color and time.
Number is unique and can be key value.

So the hash table is very recomended because I can look If number exists
just with:
_timeSpotHashTable.Contains(number)

Does the arraylist has something similar?

How can I do know, when I have 2 values to store for each number: number and
its color and time.

Thank you for your answer,

Simon


Jan Tielens said:
Would you prefer a hashtable over an arraylist is this scenario? Because a
hashtable additionally needs a key object, and my guess this is not used in
this scenario...

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
In that case, I would also recommend a HashTable (in your first question you
only needed to store a number). An arraylist doesn't have a key value, so
you probably can't use it in this scenario.

To store multiple values for each number, I would recommend to create a
class to do that:
Public Class MyItem
Private _color As String
Private _time As DateTime

Public Sub New(ByVal color As String, ByVal time As DateTime)
Me.Color = color
Me.Time = time
End Sub

Public Property Color() As String
Get
Return _color
End Get
Set(ByVal value As String)
_color = value
End Set
End Property


Public Property Time() As DateTime
Get
Return _time
End Get
Set(ByVal value As DateTime)
_time = value
End Set
End Property
End Class

You can use this class in combination with a HashTable like this:
Dim ht As New Hashtable

'Add items
ht.Add(1, New MyItem("yellow", Now))
ht.Add(2, New MyItem("green", Now))

'Check if key exists
If ht.Contains(1) Then
'...
End If

'Retrieve values
MessageBox.Show(CType(ht(1), MyItem).Color)


--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


simon said:
Now I have 3 values: number,color and time.
Number is unique and can be key value.

So the hash table is very recomended because I can look If number exists
just with:
_timeSpotHashTable.Contains(number)

Does the arraylist has something similar?

How can I do know, when I have 2 values to store for each number: number and
its color and time.

Thank you for your answer,

Simon
 
Hi Jan,

And it is so easy with a datatable with a key

dr = dt.rows.find(number)
if dr is nothing then
newrow
end if

But the hastable is in the spotlight this weeks.

But dont correct it again, you did enough to help him I thougth.

:-))

Cor
 
Hi Cor

I agree a DataTable would do the trick too, but in my opinion it would be a
little bit of overkill in this situation (I'm an OO type of guy ;-).

Cu around!
 
Back
Top