Cor,
I would be happy to do so for you. First, allow me to explain the
latest on this problem. I have traced and discovered that, before it ever
instantiates the array within the constructor, it calls the MyBase.New()
constructor. This is for class CTwoDimensionalShape, which in turn also
calls MyBase.New(). This, then, is for class CShape. The CShape
constructor attempts to WriteLine Me (which, of course, calls
Me.ToString() ). Unfortunately, the "Me" that is being referred to is
actually triangle, the object of class CTriangle! Since VB is still running
the original "MyBase.New()" step, it has not yet instantiated the array.
Okay, so I decide to try instantiating the array within the declaration
at the beginning of the class, then assigning it the proper values within
the constructor, so that at least it has something to work with for the
ToString method. Only problem is, it still crashes. When I put a
breakpoint at the declaration, it never stops, seemingly indicating that
it's not even being declared (which is odd, since other members are declared
properly before the constructor -- or at least, they're able to be accessed,
whether directly or via property, within constructors normally). I am at my
wit's end with this problem. I will post the entire CTriangle class here;
if you need me to post the other classes for you, I am happy to do so.
-- Eric
' ************************************************** ********************
' * File : Triangle.vb *
' * Author : Eric A. Johnson *
' * Date : 09/10/05 *
' * Purpose: A derived class that defines a triangle. *
' ************************************************** ********************
Public Class CTriangle
Inherits CTwoDimensionalShape ' A triangle is a 2d shape
' All triangles have three sides
Private Const mSides As Integer = 3
' Array containing the length of each side
Private mSideLength As Integer() = _
New Integer() {1, 1, 1}
' default constructor
Public Sub New()
' default, implicit call to CShape constructor is okay
' because it automatically defines the shape with 3 sides
' define each side as having length 1
' use mSides - 1 due to starting at 0
' mSideLength = New Integer(mSides - 1) {1, 1, 1}
Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New
' constructor for equilateral triangle
Public Sub New(ByVal sideSize As Integer)
MyBase.New(mSides)
'mSideLength = New Integer(mSides - 1) _
'{sideSize, sideSize, sideSize}
Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New equilateral triangle
' constructor for isosceles triangle
Public Sub New(ByVal isoscelesSide As Integer, _
ByVal oddSide As Integer)
MyBase.New(mSides)
'mSideLength = New Integer(mSides - 1) _
'{isoscelesSide, isoscelesSide, oddSide}
Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New isosceles triangle
' constructor for scalene triangle
Public Sub New(ByVal side1 As Integer, _
ByVal side2 As Integer, ByVal side3 As Integer)
' ERROR: The next statement calls the New statement for
' CTwoDimensionalShape, which calls New for CShape. That,
' in turn, tries to WriteLine Me. I have finally figured
' out in Debug mode that the Me that is accessed is
' Triangle, *not* Shape, even though Shape is calling the
' Me reference. Since mSideLength is not yet instantiated,
' it invariably crashes here. Furthermore, I have discovered
' that I cannot change the location of MyBase.New; it has to be
' the first executable line.
' POSSIBLE SOLUTION: Instantiate the array only once, right after
' it is created; then simply change what it equals later inside
' the constructor. Will test this idea now.
MyBase.New(mSides)
SideLength(0) = side1
SideLength(1) = side2
SideLength(2) = side3
Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New scalene triangle
' finalizer overrides version in class CTriangle
Protected Overrides Sub Finalize()
Console.WriteLine("CTriangle finalizer: {0}", Me)
MyBase.Finalize()
End Sub ' Finalize
' return String representation of CTriangleShape
Public Overrides Function ToString() As String
' use MyBase reference to return CShape String
Return MyBase.ToString & ";" & vbCrLf & _
"Size of sides: " & SideLength(0).ToString '& _
' ", " & mSideLength(1).ToString & ", " & _
' ", and " & mSideLength(2).ToString
End Function ' ToString
' Property for the length of a side
Public Property SideLength(ByVal sideNumber As Integer) As Integer
Get
Return mSideLength(sideNumber)
End Get
Set(ByVal Value As Integer)
mSideLength(sideNumber) = Value
End Set
End Property
End Class ' CTriangle
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Eric,
Can you first past code in a notebook, cut it and than paste it in your
message. At least in OE is this at least for me very difficult to get an
idea from the code.
Thanks
Cor