Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomize Collection

Status
Not open for further replies.

need2progm

Programmer
Dec 13, 2002
92
US
I am trying to randomize a collection before populating a dropdown box.

.net has a Random class.. I can not have any repeated data

As it stands now I have a class in my business layer where I create a collection in a property that is reference by an aspx page. This is where I would like to randomize.. before it gets returned.

Business layer class obj..
Try
SqlSelectMGID(MatchGroupID)
'Loop and add to collection
For Each drAccount In OneDS.Account.Rows
newCollection.Add(New Account(drAccount,User))
Next
Catch ex As Exception
Throw ex
End Try

'This is where I would like to randomize .. Would an ArrayList have more functionality that would make this easier?

Return newCollection

Away from programming 101 ..there has to be a better way then creating a temp obj to hold objs and write a function blah..blah..blah

Thanks for any thoughts
 
I was able to do this by using the Random class

In my business layer objects I have a class Account.vb
that Implements IComparable

' a random number generator
Private Shared random As New random()

' the random number for this answer
Private rand As Integer = random.Next()

In the constructor...
'create a random number generator to be shared by all Accounts
random = New Random()

'implement CompareTo for IComparable interface
Public Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo

'ask the random numbers in each objects to compare themselves
Return rand.CompareTo(CType(obj, Account).rand)
End Function


In the aspx page... (code behind)

'the arrayList was filled by sql call ..contains 10+ data rows

Dim randomize As New ArrayList()

'Then I can say
randomize.Sort()


It works GREAT !!! no repeated data
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top