/ Forside / Teknologi / Udvikling / VB/Basic / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
VB/Basic
#NavnPoint
berpox 2425
pete 1435
CADmageren 1251
gibson 1230
Phylock 887
gandalf 836
AntonV 790
strarup 750
Benjamin... 700
10  tom.kise 610
Array hvordan
Fra : Nyberg


Dato : 23-10-04 07:28

Hej

Jeg er ved at lave et Array hvor jeg først fylder arrayet ud eks. 1..10 det
går også godt.
Men når arrayet er fyld ud skal jeg smide den ædlest værdig ud og sætte en
ny ind .

eks der står 1 2 3 4 5 6 7 8 9 så smider jeg en ud og en ny ind så står der
2 3 4 5 6 7 8 9 67 ( bobler op i arrayet ).

Findes der en funktion array klasen til det i VB.NET eller hvordan laver
man det bedst ?

MVH

René Nyberg




 
 
Tomas Christiansen (23-10-2004)
Kommentar
Fra : Tomas Christiansen


Dato : 23-10-04 09:09

Nyberg skrev:
> Findes der en funktion array klasen til det i VB.NET eller hvordan laver
> man det bedst ?

Har du prøvet at spørge i dk.edb.programmering.dotnet, hvor der diskuteres
..NET ting og sager?

-------
Tomas


Lars Holm Jensen (23-10-2004)
Kommentar
Fra : Lars Holm Jensen


Dato : 23-10-04 14:36


> Jeg er ved at lave et Array hvor jeg først fylder arrayet ud eks. 1..10
> det
> går også godt.
> Men når arrayet er fyld ud skal jeg smide den ædlest værdig ud og sætte
> en
> ny ind .
>
> eks der står 1 2 3 4 5 6 7 8 9 så smider jeg en ud og en ny ind så står
> der
> 2 3 4 5 6 7 8 9 67 ( bobler op i arrayet ).

Jeg har selv skullet bruge en sådan struktur..
Her er en VB6-klasse:

'Beware that head and tail definitions aren't the same as e.g. Java or ML
definitions
Option Explicit

Private mysize As Integer 'The fixed size of the queue
Private full As Boolean 'Indicates if the queue is filled
Private myfields() As Variant 'The array that holds the values
Private top As Integer 'Pointer to the head of the queue
Private pointer As Integer 'Pointer to the next value to read with
nxt()
Private resetWhenAdding As Boolean 'Value that determines if the pointer
should be reset,
'to top index, when using add()

'Be sure to init() just after declaration
Public Sub init(ByVal size As Integer, Optional reset As Boolean = False)

'Just some initializations
mysize = size
ReDim myfields(size)
top = 0
pointer = 0
resetWhenAdding = reset
full = False

End Sub

'Adds a value to the queue and sets the top pointer to the new position
Public Sub add(ByVal value As Variant)

myfields(top) = value
top = (top + 1) Mod mysize
If top = 0 Then full = True
If resetWhenAdding Then
pointer = top
End If

End Sub

'Returns the value which the top pointer points at
Public Function head() As Variant

head = myfields((mysize + top - 1) Mod mysize)

End Function

'Returns the value which the top pointer points at
Public Function tail() As Variant

If full Then
tail = myfields(top)
Else
tail = myfields(0)
End If

End Function

'Returns the value which the pointer points at and sets pointer to the index
of the next-oldest
Public Function nxt() As Variant

nxt = myfields((mysize + pointer - 1) Mod mysize)
pointer = (mysize + pointer - 1) Mod mysize

End Function

'Switchs the value at the current pointer position
Public Sub switch(ByVal value As Variant)

myfields(pointer) = value
'pointer = (mysize + pointer - 1) Mod mysize

End Sub

'Returns a value
'If index is 4 then fetch() returns the fifth-newest value (Because the
queue is zero-based)
'Likewise does fetch(0) return the top/head
Public Function fetch(ByVal index As Integer) As Variant

fetch = myfields((mysize + top - (index Mod mysize) - 1) Mod mysize)

End Function

'Replaces a value
'If index is 4 then replace() replaces the fifth-newest value
Public Sub replace(ByVal value As Variant, ByVal index As Integer)

myfields((top + index - 1 + mysize) Mod mysize) = value

End Sub

'Tells if the queue is full
Public Function filled() As Boolean

filled = full

End Function

Public Sub settotop()

pointer = top

End Sub

Public Function size()

If full Then
size = mysize
Else
size = pointer
End If

End Function


Men jeg ved ikke om du kan bruge den i VB.NET..
/Lars



Søg
Reklame
Statistik
Spørgsmål : 177458
Tips : 31962
Nyheder : 719565
Indlæg : 6408170
Brugere : 218881

Månedens bedste
Årets bedste
Sidste års bedste