Record a value in a List c# RRS feed

  • Question

  • Hello,

    I am looking for a method to record a value or keep it in an IEnumerable or a list.

    My code has a variable x = 2 for example. At the end of the pipeline the process return y = 3. So when you input 2, Y = 3.

    I want to keep the value of 3, store it in a list and than replace x by 3. Now that x = 3 , y will return a new answer like y = 5. Than store in the List (2, 3, 5 ...) Make x = 5 etc. Everytime Y return a new answer it is send back to X and the value of Y is kept in a list.

    What method could allow me to do that?

    Thank you

    Monday, October 19, 2020 2:12 PM

All replies

  • Hello,

    Can you present what has been attempted so far by placing code in a code block and add any additional details such as what is the trigger to store data.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Monday, October 19, 2020 2:54 PM
  • E.g. a List<int> yourValues. Then you can add your variable, yourValues.Add(2), yourValues(3), etc.

    From a OO perspective, maybe you're talking about an observable.

    Monday, October 19, 2020 3:16 PM
  • Hello,

    I tried this but its not quite what i want.

    using System;
    using System.Linq;
    using System.Collections.Generic;
    public class Program
    	public static void Main()
    		  double number1 = 0;
            var listQ = new List<double>() {1, 3, 3.2, 4, 4.1 ,5};
            double i = 3.7;
    		int loops = listQ.Count();
    		double previousNumber = i;
    		for(int iterator = 0; iterator<loops; iterator++){
    			 number1 = FindClosest(listQ, previousNumber); //this is what you have
    			listQ = RemoveFromList(listQ, number1);
    			Console.WriteLine("item " +number1);
    						previousNumber = number1;
    	public static double FindClosest(IEnumerable<double> listQ, double number)
            return  listQ.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y);
    	public static List<double> RemoveFromList(List<double> listQ, double number)
    		return listQ.Where(a=>a!=number).ToList();
    In my case 

    double i = 3.7;

    will return Y = 4

    Qlist would list {3.7, 4, ...}

    Monday, October 19, 2020 7:57 PM
  • Is this the sort of thing you're after?

          static void Main(string[] args)
    			var listQ = new List<double>() { 1, 3, 3.2, 4, 4.1, 5 };
    			var results = new List<double>();
    			double x = 3.7;
    			while(listQ.Count > 0)
    				x = FindClosest(listQ, x); //this is what you have
    				listQ = RemoveFromList(listQ, x);
    			foreach (double y in results)

    Monday, October 19, 2020 10:27 PM
  • Not quite but your code is missing 2 things.

    Compilation error (line 15, col 9): The name 'FindClosest' does not exist in the current context
    Compilation error (line 16, col 13): The name 'RemoveFromList' does not exist in the current context

    No in fact i am trying to replace a variable by its value.

    fibo2 = (Close.GetValueAt(CurrentBar) - lowPrice0) / (Ncma - lowPrice0);
    //fibo2 is process in a bunch of formulas
    // at the end of the pipeline the variable nearclose return a value.
    //I want the value to replace Close.GetValueAt(CurrentBar) 
    //That will produce a new value for nearclose. 
    //but i cannot replace Close.GetValueAt(CurrentBar)  by nearclose otherwise i will end up in a circle.
    //it would look something like:
    fibo2 = (9 - lowPrice0) / (Ncma - lowPrice0);
    nearclose = 10
    fibo2 = (10 - lowPrice0) / (Ncma - lowPrice0);
    nearclose = 11
    fibo2 = (11 - lowPrice0) / (Ncma - lowPrice0);
    nearclose return 12 ...
    // until there is no bar left to process.

    Tuesday, October 20, 2020 12:26 PM
  • I only posted the code I changed. I meant FindClosest and RemoveFromList to be as per your post. Which I thought was rather obvious, but was clearly mistaken.
    Tuesday, October 20, 2020 10:25 PM
  • It was a bad example anyway. sorry

    Still dont know how i am going solve my problem.

    Wednesday, October 21, 2020 12:33 PM
  • Hi Frankdot,

    Judging from your current problem description, Ante's code seems to be correct, but since it is not right now, perhaps you should re-describe your problem.

    We have to really figure out your request before we can try to help,

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, October 23, 2020 8:47 AM