Trouble with Global Array of custom Class

I’m trying to create a global array of Class ‘Ring’. After some reading, I’ve done the following (all in the same Module).

  1. created a class ‘Ring’ with several properties (e.g. insideDiameter, outsideDiameter).
  2. create a (global) property called Rings() of type Ring.
  3. created a method that populates Rings() by declaring a private variable TempRing of type Ring. Once tempRing is computed I append this to Rings() with Rings.AddRow(TempRing).

Compiles and runs fine. I can add rows without issue. But all of the rows are of the same value … the last value I add. Seems every Addrow is overwriting all of the rows with the same data.

I’ve tried numerous ways to declare things differently and none of those will even compile. I note when I’m trying to do the exact same thing without the need for an array, I need the statement myRing = new Ring’ in code. But I’ve been unable to come up with the array version of this.

What am I doing wrong? Thank you.

Please show your code for 3. It looks like you always add the same class again and again.

’ builds a set of rings using MySettings MyShape

Var rowCount As Integer = 0
Var tempRing As New Ring
Var currentElevation As Double
'for calling getBezierX
var inside, outside, baseInside, baseOutside as double
var success as boolean

'get the first base inside and outside radi
success = getBezierX(0,baseInside,baseOutside)

Do Until rowCount>20

tempRing.ringNumber = rowCount
If rowCount = 0 Then
tempRing.thickness = mySettings.baseRingThickness
Else
tempRing.thickness = mySettings.ringThickness
End If

currentElevation = currentElevation + tempRing.thickness
if currentElevation > mysettings.bowlHeight then exit 'do not add rings above bowl height (above bezier curve def)

tempRing.topElevation = currentElevation
tempRing.numberOfSegments = mySettings.numberOfSegments
tempRing.offsetDegrees = 0
success = getBezierX(currentElevation, inside, outside)

'in vase shapes, base of segment could be the defining dimension. So we need min for inside and max for outside
tempRing.insideRadius = max(min(inside,baseinside) - mySettings.wallMargin,0) 'cannot be less than zero
tempRing.outsideRadius = max(outside,baseoutside) + mySettings.wallMargin

rings.AddRow(tempRing)

MainWindow.RingList.AddRow( _
rowCount.tostring, _
Format(tempRing.thickness,"#.000"), _
Format(tempRing.topelevation,"#.000"), _
tempRing.numberOfSegments.tostring, _
Format(tempRing.offsetDegrees,"#.0"), _
Format(tempRing.insideRadius,"#.000"), _
Format(tempRing.outsideRadius,"#.000") _
)

'top of this ring is bottom of next ring
baseinside = inside
baseoutside = outside

rowCount = rowCount +1
Loop

I believe tempRing’s values are correct through this loop as I am using it to populate a Listbox … that I can view.

You need to create a new instance of tempRing inside the loop.

Do Until rowCount > 20
   tempRing = New Ring
   etc...

I don’t know what the call to getBezierX does so this may be way off base but just where are inside, outside, baseInside, and baseOutside being set since it looks like you’re using the values without initializing them.

getBezierX is confusing things. It’s just a function that returns two values. sorry, I should have taken more time a posted a simplified version of this code.

As to the suggestion from Tim, I will try this. But I already have an instance of TempRing so i don’t quite understand why I need to continue to create new instances during each loop. i thought I could just overwrite the one version I created at the start of the method.

Well Tim, your suggestion made it work properly. Please tell me more about why this is needed if you have the time. I’m missing it.

Thank you,

Dan

An instance is a reference to a set of values. Each element of the array needs a separate set of values. Otherwise, they all reference the same values.

Thanks!