Abstract classes and ByVal

bpayne111

Junior Contributor
Joined
Feb 28, 2003
Messages
325
Location
BFE
OK i have a 3 classes (Chessboard, NumericBoard, Dictator) and all 3 have one common property... Pieces as PieceListBase (actually each class has a different propert(1 PieceListBase, 1 PieceListNumeric, 1 PieceListGUI) PieceLIstBase is an abstract class that inherits from ArrayList and the other 2 PieceLists inherit from the base

The Dictator takes a PieceListGUI and converts it to a PieceListNumeric

Somehow after i make the conversion the values end up applying to all instances of the PieceList classes.
ie. i'm converting the location value for each piece in the pieceListGUI to a new value and setting PieceListNumeric to my converted class (i use a function to do this)

The values are passed ByVal and i can't seem to find how these values are accumulating like they do... (i end up with an arithmatic overflow eventually)

which FINALLY brings me to my question...
Could the Base Class for my PiecesList have something to do with this? i'm posting some of the code

The overflow occurs in the ConvertToNumeric Function

HEEEEEEEEELP i'm gonna lose my mind
Visual Basic:
'chessboard class
        Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)

            Dim squareIndex As Short

            squareIndex = GetCurrentSquare(e)
            myCursor.State = True
            mDictator = New MasterDictator(mPieces)
            If Not IsNothing(mSquare(squareIndex).Image) AndAlso mDictator.PieceGrabbedIsLegal() Then

                myCursor.Image = mSquare(squareIndex).Image
                myCursor.Rectangle = New Rectangle(e.X, e.Y, iSize, iSize)
                'add previous square variable?
                mSquare(squareIndex).Image = Nothing
                Me.Refresh()
            Else
                myCursor.State = False
            End If

        End Sub

'MasterDictator class
  Public Sub New(ByVal hostPieces As PieceListBase)

            MyBase.New()
            MyBase.Pieces = MyClass.ConvertToNumeric(hostPieces)
           
        End Sub

 Private Function ConvertToNumeric(ByVal myPieces As PieceListBase) As PieceListNumeric
            'converts the locations of the pieces to the corresponding index of mNumeric.Square(x)
            Dim aPiece As Piece
            Dim numericPieces As New PieceListNumeric()

            For Each aPiece In myPieces
                If Not aPiece.Rank = Rank.Abstract Then
                    aPiece.Location = aPiece.Location + 26 + (4 * ((aPiece.Location \ 8) + 1) - 1)
                    'what in TARNATION
                    numericPieces.Add(aPiece)
                    'numericPieces.Item(aPiece.Index) = aPiece
                End If
            Next
            Return numericPieces
        End Function
 
Last edited by a moderator:
wow, i feel like an idiot. and to think i thought i was solid on that for years now.

Well you know that sucks really bad
You obviously see what i'm trying to do here so here's my stab at fixing it let me know what you think...

instead of passing the true value.... create a new value and send that one to the method instead.

ie.
Public Some Sub()
Dim myClassesProperty as ArrayList

Dim myValueToPass as ArrayList

'statements

myvalueToPass = myClassesProperty

myConverterSub(myValueToPass) 'now myClassProperty won't be effected.
End Sub
 
Try using the Arraylist.Clone method. This only does a shallow copy, but you can override the implementation, seeing that you've inherited from it, if you need to......
thus, with your above example you only need to do:
Visual Basic:
myConverterSub(myClassesProperty.Clone())
 
can you enlighten me to this term shallow copy (i've seen clone and always wondered why it was usefull, now it all comes together :) )


thanks alot
 
To quote help:
A shallow copy of a collection copies only the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to.
If you got 'complex' objects (objects that holds other objects(reference types) as properties), then you will need to exstent on the Clone function. (Make sure these other objects also gets copied)
 
Last edited:
ahh yes... i had read the help... thanks for putting that in laymans terms. i have trouble decoding these help files at times. one of you guys should write a book on using microsoft help lol

yes they are 'complex' objects... that's what i was afraid that line meant lol i was hoping for a little less work :)

thanks alot for the help
 
Back
Top