Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
my program in c# has a function that runs several times and some data has to be stored in an arraylist each time. how can i have an arraylist with all data collected in each time the function works to plot graph finally.

What I have tried:

i have tried declaring arraylist in class but i couldnt
Posted
Updated 21-Jun-21 8:11am
Comments
Richard MacCutchan 21-Jun-21 11:48am    
"i have tried declaring arraylist in class but i couldnt"
Couldn't what? Declaring at class level is the obvious answer to your question. But without seeing your actual code we have no way of telling what might be the problem.
Richard Deeming 21-Jun-21 11:49am    
Don't use the old System.Collections.ArrayList class - it pre-dates generics, and will only cause problems. Use the generic System.Collections.Generic.List<T> class instead.
Dave Kreskowiak 21-Jun-21 12:27pm    
Show the code you've tried. I get the feeling you're trying to declare all of your variables at the class-level instead of in your methods where you need them.

1 solution

First off, as Richard says, don't use - use a List<T> instead. It's much easier to use as well as safer.

Then it's just a case of declaring it in your class:
C#
private List>MyData> theList = new List>MyData>();
And then filling it:
C#
private void AddAValue(double value)
   {
   theList.Add(new MyData() {Timestamp = DateTime.Now, Value = value});
   }
 
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