-3

I have a variable called in_data which updates every second. I want to save this data into an array or list. The number of data received cannot be defined . Is there a way for me to save this indefinite number of data ????

For example lets say i have 11 data value changes for the in_data variable. I want to store all of them so that i can use that data for later manipulations.

Thank You in advance.

in_data = myport.ReadLine();

myCollection = new List();

myCollection.Add(in_data);

but this only saves the last instance of the data,, i want all the data that was received to be saved

Randi_789
  • 37
  • 2
  • 12

3 Answers3

2

You can use List object. Define it like:

List<string> list = new List<string>();

and then later just use:

list.Add(in_data);
Nema
  • 397
  • 2
  • 12
  • yes but this only saves the last instance of the in_data ,,, i want to save all the data that was received – Randi_789 Jun 02 '15 at 09:53
  • 1
    Every time you get the new value with myport.ReadLine(); you just use list.Add method to add that value to the list. The creating of list should be done only once at beggining. – Nema Jun 02 '15 at 10:00
0

It is not very clear how in_data is updated. But assuming that you have access to that code, you could create a property instead of in_data as follows:

public readonly List<string> in_data_history = new List<string>();

public string in_data
{
     get {return in_data_history.Last();}
     set {in_data_history.Add(value);}
}

The getter for this property will return the last value of in_data. The setter will store all new values in in_data_history.

Can Bud
  • 142
  • 1
  • 11
  • If i where you, i would not use linq extension method Last() to get value. It gets time proportional to namber of elements in collection. You should place current value in different variable. – Kirill Bestemyanov Jun 02 '15 at 09:52
  • in_data = myport.ReadLine(); this is the way in which in_data is being updated sir – Randi_789 Jun 02 '15 at 09:55
  • @Kirill Bestemyanov: A List already knows its length and the parameterless version of Last does not need to perform checks on each element, so I would expect it to perform in T(1) rather than T(N). Unfortunately [msdn](https://msdn.microsoft.com/en-us/library/vstudio/bb358775(v=vs.90).aspx) does not mention its performance. – Can Bud Jun 02 '15 at 09:59
  • @user2766227: If you can replace the definition of in_data with the code above, the list in_data_history will track all values of in_data. This works because whenever in_data = myport.ReadLine() is called the setter defined above is invoked adding the latest value into in_data_history. – Can Bud Jun 02 '15 at 10:02
  • @Can Bud: Cause List does not have method Last(), it will be used extension method of IEnumerable interface. So it will work for O(n). I look at realization of this method now. – Kirill Bestemyanov Jun 02 '15 at 10:26
  • @KirillBestemyanov: It seems that this issue has been discussed before, see [here](http://stackoverflow.com/questions/1377864/what-is-the-performance-of-the-last-extension-method-for-listt) and [here](http://stackoverflow.com/questions/4833220/does-calling-last-on-an-ilist-iterate-the-entire-list). – Can Bud Jun 02 '15 at 10:32
  • For this use case I think that a `Stack` would be a better suit than `List` – Bill Tür stands with Ukraine Jun 02 '15 at 10:37
  • A stack and a list are rather similar. This use case adds like a push, but it never pops or peeks. Considering that the intent is to keep a history, I believe that the access to the collection will suit a list. – Can Bud Jun 02 '15 at 10:50
0

You can read all lines with this in your string in_data:

in_data = myport.ReadToEnd();

Or you can use a loop:

myCollection = new List(); 
while ((in_data = myport.ReadLine()) != null)
{
    myCollection.Add(in_data);
}
Adelphos
  • 83
  • 7