使用一组(对象)集合 [英] Work with a collection of collections(of objects)
问题描述
我正在收集用于数据记录和绘图的PLC数据。我正在尝试创建一个集合集合,以便轻松地动态添加数据点。我在挣扎。这是正在发生的事情。我正在创建名为bigCol和dataCol的两个(新)集合。我。添加()5 dataCol到bigCol。然后我将一个PlotData对象添加到bigCol集合中的每个dataCol集合中。所以我应该有一个5个dataCol的集合,每个都有一个PlotData,但是我添加的项目太多了。这是我的代码:
I am collecting PLC data for data logging and graphing. I am trying to make a collection of collections so as to easily add data points dynamically. I am struggling. Here is what is happening. I am creating two (new) collections named bigCol and dataCol. I .Add() 5 dataCol's to bigCol. Then I add one PlotData object to each one of the dataCol collections within bigCol collections. So I should have a collection of 5 dataCol's with one PlotData in each one, but I am getting too many items added. Here is my code:
Sub Main()
Dim bigCol As New Collection()
Dim dataCol As New Collection()
Dim i As Integer
'Create a bigCol collection full of dataCol collections
'So it is a collection of collections
For i = 0 To 4
bigCol.Add(dataCol)
Next
'Add 1 plotData object to each of the dataCols in bigCol
For Each item In bigCol
dataCol.Add(New PlotData("name" & i.ToString))
i += 1
Next
Console.WriteLine("bigCol.count = " & bigCol.Count)
Dim e As PlotData
For Each item In bigCol
Console.WriteLine("bigCol Element")
Console.WriteLine("dataCol.count = " & dataCol.Count)
For Each e In dataCol
Console.Write(" {0} ", e.ToString())
Console.Write(" {0} ", e.X1)
Console.Write(" {0} ", e.Y1)
Console.Write(" {0} ", e.X2)
Console.WriteLine(" {0} ", e.Y2)
Next e
Next
PressAnyKey()
End Sub
PlotData只是一个有2个字符串属性和4个整数值的类。
这是我从控制台得到的......
bigCol.count = 5
bigCol元素
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol元素
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol元素
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol元素
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol元素
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
请按任意键继续!
PlotData is just a class with 2 string properties and 4 integer values.
Here is what I am getting from the console...
bigCol.count = 5
bigCol Element
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol Element
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol Element
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol Element
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
bigCol Element
dataCol.count = 5
name5 0 0 0 0
name6 0 0 0 0
name7 0 0 0 0
name8 0 0 0 0
name9 0 0 0 0
Please Press any key to continue!
推荐答案
您正在遍历集合并在dataCol中添加数据而不是在item中。喜欢
You are traversing through a collection and adding data in dataCol instead in item. like
For Each item In bigCol
dataCol.Add(New PlotData("name" & i.ToString))
i += 1
Next
在此代码中,您将遍历bigCol,然后该项目未在任何地方使用,它应该是这样的。
In this code you are traversing bigCol then the item is not being used anywhere, It should be like this.
For Each item In bigCol
item.Add(New PlotData("name" & i.ToString))
i += 1
Next
然后再次显示你正在做同样的显示错误。
Then again while you are displaying you are doing the same mistake of displaying.
也许我应该解释一些并得到你的想法。我正在将VB6应用程序升级到.net。这里的代码只是一个测试这个想法的实验。我收集的是温度和温度。数据记录和图表的压力数据。我的想法(以及来自VB6的应用程序)是为每个温度或压力输入(其中18个)提供一个或一系列数据点并将它们放入一个集合中,以便我可以动态地继续向每个系列添加数据甚至添加如果我需要另一个系列。也许我说这一切都错了。我愿意接受建议。
Maybe I should explain some more and get your ideas. I am upgrading a VB6 application to .net. The code here was just an experiment to test the idea. What I am gathering is temperature & pressure data for data logging and charting. My idea (and from the VB6 app) is to have a collection or series of data points for each temperature or pressure input (18 of them) and put them in a collection so that I can dynamically continue adding data to each series and even add another series if I need. Maybe I am going about this all wrong. I am open to suggestions.
请参阅我关于使用泛型集合类型的注释,而不是非泛型(非专用)类型,尤其是非标准 Microsoft.VisualBasic
哪些可能会影响您的.NET开发,将使用您在标准方式编写的其他程序集中的工作。
这是事情:这样的旧类型已经早在.NET v.2.0中,当引入泛型时,它就已经过时了。以前的.NET版本还不是真正的.NET:第一个真正的.NET版本是2.0,如果不是以后的话。泛型允许您避免容易出错的类型转换,同时不会影响性能。非泛型类型并不是正式的市场过时,只是因为将它们保存在经过良好调试的遗留代码中没有任何问题,但在新代码中它们将简直是荒谬的。
请参阅:
http:// msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/System.Collections.Generic%28v= vs.110%29.aspx [ ^ ]。
Please see my comments about using the generic collection types, instead of non-generic (non-specialized) types, especially non-standard Microsoft.VisualBasic
which can compromise your .NET development, will present using your work in other assemblies written in standard way.
Here is the thing: such old types have been rendered obsolete as early as of .NET v.2.0, when generics were introduced. And prior .NET version wasn't yet "real .NET": first "real" .NET version was 2.0, if not later. Generics allow you to avoid error-prone type casting while not compromising even a bit of performance. Non-generic types were not formally market obsolete only because there is nothing wrong with keeping them in well-debugged legacy code, but in new code they would be simply ridiculous.
Please see:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/System.Collections.Generic%28v=vs.110%29.aspx[^].
>
这篇关于使用一组(对象)集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!