English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a class (card) with various properties. I also have another class (cardcollection) which is a collection of a bunch of objects of the type card. I use a for loop to display the text property of the class in a list box of each object of the type card class.

what i want to do is click a button and a text box changes to the text property of a random object within the cardcollection class.

if this is more than can be explained here then please give me the words i need to Google to do this. i know .net has things built in for randomization, i just don't know how to use it, or what it is called to research it.
thanks,

2007-01-23 18:49:58 · 1 answers · asked by Jamie J 3 in Computers & Internet Programming & Design

The code i use to view objects in the different objects of type "card" class; in a combo box, list box, and 3 text boxes. this part does not need randomization. another part of the app does.

Dim _cardcollec As CardCollection
For i As Integer = 0 To _cardcollec.Count - 1
Dim _card As Card
_card = _cardcollec.Item(i)

If Not cmbCategory.Items.Contains(_card.Category) Then
cmbCategory.Items.Add(_card.Category) 'adds catagories to combo box
End If
If _card.Category = cmbCategory.SelectedItem Then
lstCardList.Items.Add(_card)
End If
Next

2007-01-23 18:52:30 · update #1

1 answers

//What you basically miss is getting a random number between 0 //and CardCollection.Count -1.

Random RandomObject = new Random();
int RandomIndex = Random.Next(0, _CardCollection.Count-1);

//You can this retrieve the your card object from the collection by //using
MyRandomCard = MyCardCollection[RandomIndex];

//play with your MyRandomCard instance now
......

Note: The sequence of values returned by the Random class are not truly random, because they're generated by a definite mathematical algorithm wherein the previously selected value is used in calculating the next number.

For More Info Check how to seed Random Numbers using the Random Class

Thank You

2007-01-23 21:14:15 · answer #1 · answered by Smutty 3 · 0 0

fedest.com, questions and answers