Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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 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!
Posted
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 19:54pm    
What is collection? Full type name please. You need to use some generic type. "Collection of collections", by definitions, means two different types, but you show "new Collection()" twice, which is only possible for untyped collections (of System.Object), which is bad. That's the key.
—SA
ScottTheHunter 19-Aug-14 17:02pm    
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.
Sergey Alexandrovich Kryukov 19-Aug-14 17:15pm    
The real solution is to avoid using the loose-type class Microsoft.VisualBasic.Collection (and to avoid using Microsoft.VisualBasic at all, preferring only the standard assemblies). It could be System.Collections.Generic.List<> or other generic collections. And... this is really, really simple.
—SA
ScottTheHunter 19-Aug-14 17:33pm    
OK, I will look into using Lists instead of Collections. Thanks so much for your help. Keep an eye out for questions about Lists as I have not used them before.
Sergey Alexandrovich Kryukov 19-Aug-14 18:17pm    
This issue is so important that I posted a separate formal answer, Solution 2.
Your follow-up questions will be welcome; I hope you will formally accept it and use in your work (green "Accept" button).
—SA

You are traversing through a collection and adding data in dataCol instead in item. like
VB
For Each item In bigCol
            dataCol.Add(New PlotData("name" & i.ToString))
            i += 1
        Next

In this code you are traversing bigCol then the item is not being used anywhere, It should be like this.


VB
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 23:08pm    
The real solution is to avoid using the loose-type class Microsoft.VisualBasic.Collection (and to avoid using Microsoft.VisualBasic at all, preferring only the standard assemblies). It could be System.Collections.Generic.List<> or other generic collections.
—SA
Mayank Vashishtha 18-Aug-14 23:12pm    
Agreed! Just trying to give a logical reason of problem. :-)
Member 11010889 wrote:
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.
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[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900