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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection class question 2

Status
Not open for further replies.

foundryqa

Technical User
Joined
Oct 17, 2001
Messages
162
Location
US
Guys and gals,

Please bear with me, because I've just taken the OO plunge and made my first class. It is a "die" class, used for gaming. I created a roll method and a constructor that requestes how many sides the die should have. I've also made a "dice" collection class. I was hoping to make a similar "roll" method that would create a new random value for each die in the collection. I thought that this would be written in the dice class as a function called roll with terminology something like:
Code:
for each die in dice
... code here
next die
That gives me enough errors that I now feel that is the wrong way to go. So, my question is, what is the syntax for a function in a collection class that needs to act on each of the members of the class?

The search engine for the site is down tonight so if this has been answered in the past, my apologies.

Thanks for the help,

Fred
 
assuming you have a class die and it has a public function .roll()

Code:
    Dim die1 As New die()
    Dim die2 As New die()
    Dim die3 As New die()
    Dim dice As New Collection()
    dice.Add(die1)
    dice.Add(die2)
    dice.Add(die3)
    Dim itererator As die
    For Each itererator In dice
      Debug.WriteLine(itererator.roll())
    Next

-Rick

----------------------
 
Rick,

Thanks for the response. I cuurently can do as you say. I have a dice collection object defined in a class which you can add die to. What I would like to do is add a method to the dice object that would allow me to type dice.roll and each member of dice would roll. Your solution would work fine, I'm just trying to learn more about creating classes and so forth and thought it would be a good challange to add a function directly into the dice class that would achive the roll for dice that I currently have for die.

Thanks,

Fred
 
Public Sub Roll()
For Each d As Dice In List
d.Roll()
Next
End Sub
 
Thanks RiverGuy,

I'll try it tonight when I get home. Question, is List inherited from the collectionbase? And, I've never defined the object right in a for each statement before, that is what the d as dice is doing right?

Thanks,

Fred
 
1. Yes.
2. That works in 2003, not 2002. If using 2002, define it first.
 
I made a custom class Dice that works as a collection for Die. it has a roll method:

Code:
Public Class dice
  Inherits System.Collections.ArrayList

  Public Function Roll(ByVal NumberOfDice) As Integer()
    Dim i As Integer
    Dim ReturnVal(NumberOfDice - 1) As Integer
    For i = 0 To NumberOfDice - 1
      Me.Add(New die())
    Next i

    Dim dieIterator As die
    i = 0
    For Each dieIterator In Me
      ReturnVal(i) = dieIterator.roll()
      i += 1
    Next
    Return ReturnVal
  End Function

End Class

my die class is as follows:
You can easily add a constructor and a private variable to set the number of sides also
Code:
Public Class die
  Public Function roll() As Integer
    Return (Rnd() * 6) \ 1 + 1
  End Function
End Class

And the code to use it looks like this:
Code:
    Dim MyDice As New dice()
    Dim i() As Integer = MyDice.Roll(6)

-Rick

----------------------
 
Rick, I'm assuming he's inheriting from CollectionBase, and using the examples in the "Creating your own collection class" series on MSDN.
 
RiverGuy and Rick,

Thank you very much. When I get home tonight, I will chew on all of this. It helps having people around like yourselves that are willing to show someone new to these things.

Fred
 
RG - could be, there are atleast 101 different ways to build a dice roller in .net.

Foundry - thank my boss, it was a slow start to the morning, and you never know when a dice roller will come in handy. Maybe I'll stick it in our leasing application as an easter egg. Let those sales guys throw some bones over lunch.

-Rick

----------------------
 
You never know, they may end up using it to determine their sales forcast.

Foundry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top