Reputation: 41
I successfully get the list of the record but the problem is the last record always override the first record. The record count already correct. Here are my codes. Please help. Thanks.
Private Function MyFunction(ByVal myList As List(Of TT_GENERAL_CONFIGURATION))
Dim pConfig As New Config
Dim lst As New List(Of Config)
For Each MyCls As TT_GENERAL_CONFIGURATION In myList
pConfig.ConfigID = MyCls.INTERNAL_NUM
pConfig.ConfigType = MyCls.CONFIG_TYPE
pConfig.ConfigName = MyCls.CONFIG_NAME
pConfig.ConfigValue = MyCls.CONFIG_VALUE
lst.Add(pConfig)
Next
Return lst
End Function
Upvotes: 0
Views: 85
Reputation: 1881
you are instanciating pConfig
only one time, and then in you loop you are modifying it over and over again move the instanciation eithin the loop.
Upvotes: 0
Reputation: 5128
That's because you're reusing the same reference to an object which means you're not actually adding new objects but rather new references to the same object. Your code should look like the following (the initialization of "Config" type is moved inside the loop):
Private Function MyFunction(ByVal myList As List(Of TT_GENERAL_CONFIGURATION))
Dim pConfig As Config
Dim lst As New List(Of Config)
For Each MyCls As TT_GENERAL_CONFIGURATION In myList
pConfig = New Config()
pConfig.ConfigID = MyCls.INTERNAL_NUM
pConfig.ConfigType = MyCls.CONFIG_TYPE
pConfig.ConfigName = MyCls.CONFIG_NAME
pConfig.ConfigValue = MyCls.CONFIG_VALUE
lst.Add(pConfig)
Next
Return lst
End Function
Hope this helps.
Upvotes: 0
Reputation: 236238
Currently you create single config instance and add it many times to your list. On each loop you only modify values of this single config object. Move config creation inside the loop:
Private Function MyFunction(ByVal myList As List(Of TT_GENERAL_CONFIGURATION))
Dim lst As New List(Of Config)
For Each MyCls As TT_GENERAL_CONFIGURATION In myList
Dim pConfig As New Config
pConfig.ConfigID = MyCls.INTERNAL_NUM
pConfig.ConfigType = MyCls.CONFIG_TYPE
pConfig.ConfigName = MyCls.CONFIG_NAME
pConfig.ConfigValue = MyCls.CONFIG_VALUE
lst.Add(pConfig)
Next
Return lst
End Function
Upvotes: 2