cancel
Showing results for 
Search instead for 
Did you mean: 

adding table entries

Former Member
0 Kudos

Hello all,

When i add table entries I get i times the last entry i added.

Here is the code:

Overrides Protected Sub Zrfc_Projectdata_Import ( _

ByVal Aufnr As String, ByRef Test As String, ByRef Kostprj_Tab As ZPRJAUERTable)

' TODO: add your server code here

Dim i

Dim Kostprj As New ZPRJAUER

For i = 1 To 10

Kostprj.Bmnr = i

Kostprj_Tab.Add(Kostprj)

Next

End Sub

Am i doing anything wrong here?

Thanks in advance

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hello,

You are filling the table with the same table item created outside the loop. You need to create a new item for each table row inside the loop. The code looks like:

For i= 1 to 10

Kostprj = New ZPRJAUER

...

Kostprj_Tab.Add(Kostprj)

Next

Regards,

Guangwei Li

reiner_hille-doering
Active Contributor
0 Kudos

Move the "new New ZPRJAUER" inside of the loop:

Dim i

Dim Kostprj As ZPRJAUER

For i = 1 To 10

Kostprj = New ZPRJAUER

Kostprj.Bmnr = i

Kostprj_Tab.Add(Kostprj)

Next

This is necessary because ZPRJAUER is a so called "reference type" object - Kostprj_Tab holds a pointer to it. If you don't create new instances for each table row, all rows would point to the same instance of ZPRJAUER.