Help with class array

Hello all.
In the following code each time I append the array, a new element is created but ALL existing elements are changed to match the values of the new array. Can anyone see what I am doing wrong?

Thanks,
Tim

  For n As Integer = 0 To App.Wireless_Device(i).Wireless_Dev_Prop.Ubound
    For DayNo = 1 to 365
      For n = 0 to App.Wireless_Device(i).Wireless_Dev_Prop.Ubound
        
        If App.Wireless_Device(i).Wireless_Dev_Prop(n).EventDate.DayOfYear = DayNo Then   'Day Of Year
          tD = App.Wireless_Device(i).Wireless_Dev_Prop(n).EventDate
          Day_Cnt  = Day_Cnt  + 1
          Cnt_Idx = Cnt_Idx + 1
          Cnt = Cnt + 1
          Day_Hr_Cnt  = Day_Hr_Cnt  + 1
          Day_Ave_V = Day_Ave_V +App.Wireless_Device(i).Wireless_Dev_Prop(n).Voltage
        End If
      Next n
      
      If Day_Cnt  > 0 Then 
        Day_Ave_V = Day_Ave_V / Day_Cnt 
         r1.Date_Day = tD  
        r1.Ave_Voltage = Day_Ave_V
        r1.Count = Day_Cnt 
        App.Wireless_Device(i).Results_Day.Append R1   //<<======  Changes all array elements to the same as the new one!
        // App.Wireless_Device(i).Results_Day(tN).Date_Day = tD
        // tN = tN + 1
      End If
      Day_Cnt   = 0
    Next DayNo
    
    HrCnt = 0
    Day_Ave_V = 0
    Day_Hr_Cnt  = 0
    Day_Cnt  = 0
  Next n
  

I dont see where you ever create a new date
You seem to just refer to the existing one (td)
So I bet all your entries have the same reference

In the preferences turn on Debugging > Show Object ID’s in variable lists
you can tell this IF you put in break point and
then in the loop put a break point & inspect several and IF the object id shown is the same for several thats your problem

Hi Norman,

tD is changing but what I did find was that the append is generating the same object ID each time for the array clsResults_Day.
The values in R1 are different each time as well, which is what I am using to append with.
App.Wireless_Device(i).Results_Day.Append R1

Thoughts?

I found it. I needed to create a new instance of r1!

Dim r1 As New clsResults_Day

If Day_Cnt  > 0 Then 
  Day_Ave_V = Day_Ave_V / Day_Cnt 
  Dim r1 As New clsResults_Day
  r1.Date_Day =tD    //App.Wireless_Device(i).Wireless_Dev_Prop(n).EventDate
  r1.Ave_Voltage = Day_Ave_V
  r1.Count = Day_Cnt 

Thanks for the hint Norman!
Tim

Your using n as loop variable in two For loops:

For n As Integer = 0 To App.Wireless_Device(i).Wireless_Dev_Prop.Ubound For DayNo = 1 to 365 For n = 0 to App.Wireless_Device(i).Wireless_Dev_Prop.Ubound

And R1 needs to be defined within the inner loop:

... If Day_Cnt > 0 Then Day_Ave_V = Day_Ave_V / Day_Cnt Dim r1 As New clsResult() <–––––––––– r1.Date_Day = tD r1.Ave_Voltage = Day_Ave_V r1.Count = Day_Cnt App.Wireless_Device(i).Results_Day.Append R1 ...

Hi Eli
We must have been writing at the same time!
Thanks for your response.

Tim

[quote=240320:@Tim Seyfarth]Hi Norman,

tD is changing but what I did find was that the append is generating the same object ID each time for the array clsResults_Day.
[/quote]
Thats exactly what I said - your reusing the SAME date object all the time so changing one appears to change them all