列表列表vb .net我似乎无法创建列表列表,请你帮忙 [英] list of list vb .net I cant seem to create list of list can you please help

查看:85
本文介绍了列表列表vb .net我似乎无法创建列表列表,请你帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建列表列表,我希望有类似下面的内容,但每次将tempList添加到myList。一个strage的事情发生,整个列表被新的tempList覆盖,整个myList包含相同的List只有tempList



im trying to create list of list, i want to have something like below but every time the tempList is added to myList. a strage thing happens, the ENTIRE LIST get overwritten with the new tempList and the entire myList contains the same List that are only tempList

Dim MsgList As New List(Of IList(Of String))()
   Public Sub getAllDBCMsgSigs()
       'Dim d As New OpenFileDialog()
       'd.ShowDialog()
       'getMsgs(d.FileName)

       Dim line As String
       Dim fileReader As New StreamReader("1dbcMsrgSigs.txt")
       Dim tempList As New List(Of String)
       MsgList.Add(tempList)
       Do While fileReader.Peek() > 0
           line = fileReader.ReadLine

           If line.Length > 0 Then
               tempList.Add(line)
           Else
               MsgList.Add(tempList)
               tempList.Clear()
           End If
       Loop
   End Sub





你能告诉我我在做什么错误



提前感谢



could you please tell me what im doing wrong

thanks in advance

推荐答案

你需要移动

You need to move the
Dim tempList As New List(Of String)

内线你的循环。

目前,你正在将相同的列表添加到MsgList中每次,他们清除所有条目。

如果你想要一个字符串列表列表,你每次都要创建一个新的字符串。



我不知道为什么你虽然这样做 - 这似乎是一个很长的路...

line inside your loop.
At the moment, you are adding the same list into MsgList each time, and them clearing all the entries.
If you want a list of lists of strings, you must create a new one each time.

I don't know why you are doing it that way though - it seems a very long way round...


是的,所以我在我的tempList中创建一个字符串列表,然后当列表完成后我把它放将整个列表放入myList,然后我再次开始创建一个字符串的tempList。



只需创建一个2D字符串数组



i只是不明白它为什么要记录我的整个myList。

一旦tempList被添加到myList.add ..(即myList(0)获取tempList)为exanmple

它应该不能覆盖它,除非我说myList0)= tempList
yes, so i create a list of string in my tempList, then when the list is done i put the entire list into myList, then I start again creating a tempList of strings.

simply im creating a 2D array of strings

i just dont understand why it is oferwriting my entire myList.
once the tempList has been added to myList.add..(i.e. myList(0) get tempList) for exanmple
it should not be able to oversrite it unless i say myList0)=tempList


使用 Activator.CreateInstance (Of List(Of String)) ie

Use Activator.CreateInstance(Of List(Of String)) i.e.
Dim outerList as List(Of List(Of String))

For i as Integer = 0 to 1000
    outerList.Add(Activator.CreateInstance(Of List(Of String)))
Next



这会创建 outerList ,其中包含1000个匿名列表(字符串),

每个只能通过他们在 outerList 中的索引。



请参阅:http://msdn.microsoft.com/en-us/library/system.activator.createinstance(v=vs.100).aspx [ ^ ]


This creates outerList with 1000 anonymous Lists(Of String),
each being only accessible by their index in outerList.

See: http://msdn.microsoft.com/en-us/library/system.activator.createinstance(v=vs.100).aspx[^]


这篇关于列表列表vb .net我似乎无法创建列表列表,请你帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆