locked
Can we initialize multiple values in a for loop? RRS feed

  • Pergunta

  • Hello,

    I have a variable Y returning values 1,2,3,4.

    Is it possible to initialize Y in a for loop and get the return for each values?

    for(int i = 0; i < 10; i++)

    Replace 0 by Y and it will return in one shot without having to repeat 4 For loops or create 4 variables like Y, U, P, X..

    1,2,3,4,5,6,7,8,9, 10

    2,3,4,5,6,7,8,9,10

    3,4,5,6,7,8,9,10

    4,5,6,7,8,9,10

    Thank you


    • Editado Frankdot quarta-feira, 19 de agosto de 2020 12:27
    quarta-feira, 19 de agosto de 2020 11:56

Todas as Respostas

  • Assuming this is an int array

    int[] arr = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    Console.WriteLine(string.Join(",", arr));
    for (int i = 0; i < 3; i++)
    {
        arr = arr.Skip(1).ToArray();
        Console.WriteLine(string.Join(",", arr));
    }

    Console.WriteLine and string.Join is for you to see the results

    Another idea

    int[] arr = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    var arrCopy = new int[arr.Length];
    
    arr.CopyTo(arrCopy,0);
    
    Console.WriteLine(string.Join(",", arrCopy));
    for (int i = 0; i < 3; i++)
    {
        arrCopy = arrCopy.Skip(1).ToArray();
        Console.WriteLine(string.Join(",", arrCopy));
    }


    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.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    quarta-feira, 19 de agosto de 2020 12:45
  • i dont get how you can make it work with the skip. Another complication is i cant use an array but i can use List.

    If i try on https://dotnetfiddle.net/

    it returned an error:

    ompilation error (line 16, col 23): 'System.Array' does not contain a definition for 'Skip' and no extension method 'Skip' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
    Last Run: 9:01

    quarta-feira, 19 de agosto de 2020 13:07
  • What is the exact objective and how would you want your 4 loops returned? E.g. which type is holding these extra arrays (another array ?)

    Also, why do you want to avoid an extra outer loop (or do while, for example)?


    Looking for new opportunities

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    quarta-feira, 19 de agosto de 2020 13:12
  • In regards to the following, means you are using an older version of the .NET Framework, try with say 4.0 or above.

    compilation error (line 16, col 23): 'System.Array' does not contain a definition for 'Skip' and no extension method 'Skip' 
    accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

    here is with a list

    var originalList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    var copyOfOriginalList = new List<int>(originalList);
    
    Console.WriteLine(string.Join(",", copyOfOriginalList));
    for (int index = 0; index < 3; index++)
    {
        copyOfOriginalList.RemoveAt(0);
        Console.WriteLine(string.Join(",", copyOfOriginalList));
    }
    1,2,3,4,5,6,7,8,9,10
    2,3,4,5,6,7,8,9,10
    3,4,5,6,7,8,9,10
    4,5,6,7,8,9,10


    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.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    quarta-feira, 19 de agosto de 2020 13:12
  • I tried this:

    int totalI = 0;
    				
    				
    				for(int barIndex = ChartBars.FromIndex; barIndex <= index; barIndex++)
    				{
    					totalI = barIndex;
    					
    					Print("totalI" + totalI);
    					
    		// totalI include the bars in the chart from left to middle.			
    		// so i tried to apply your solution. 			
    					
    					var originalList = new List<int>() {totalI};
    					var copyOfOriginalList = new List<int>(originalList);
    
    					Print(string.Join(",", copyOfOriginalList));
    					for (int indes = 0; indes < totalI; indes++)
    					{
    					    copyOfOriginalList.RemoveAt(0);
    					    Print(string.Join(",", copyOfOriginalList));
    					}
    					
    					
    					
    					
    					
    					
    					
    					
    					
    					
    				}
    				
    				


    Still getting an error related to the program. This is not a compiling error but an error coming from the output window. 

    Indicator 'NEWSDBA': Error on calling 'OnRender' method on bar 258: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    There is 258 bars in the chart. But the loop only include bar 0 to bar 152. 

    Frank

    Thank you

    quarta-feira, 19 de agosto de 2020 13:40
  • I tried this:

    int totalI = 0;
    				
    				
    				for(int barIndex = ChartBars.FromIndex; barIndex <= index; barIndex++)
    				{
    					totalI = barIndex;
    					
    					Print("totalI" + totalI);
    					
    		// totalI include the bars in the chart from left to middle.			
    		// so i tried to apply your solution. 			
    					
    					var originalList = new List<int>() {totalI};
    					var copyOfOriginalList = new List<int>(originalList);
    
    					Print(string.Join(",", copyOfOriginalList));
    					for (int indes = 0; indes < totalI; indes++)
    					{
    					    copyOfOriginalList.RemoveAt(0);
    					    Print(string.Join(",", copyOfOriginalList));
    					}
    					
    					
    					
    					
    					
    					
    					
    					
    					
    					
    				}
    				
    				


    Still getting an error related to the program. This is not a compiling error but an error coming from the output window. 

    Indicator 'NEWSDBA': Error on calling 'OnRender' method on bar 258: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    There is 258 bars in the chart. But the loop only include bar 0 to bar 152. 

    Frank

    Thank you

    Actually, it looks like this:

    totalI0
    0
    totalI1
    1
    totalI2
    2

    totalI3
    3

    Indicator 'NEWSDBA': Error on calling 'OnRender' method on bar 258: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    quarta-feira, 19 de agosto de 2020 13:56
  • I suggest if you have not already done so is to setup breakpoints and step through the code, examine values for the variables. So the first thing to examine is the part about index out of range, examine the counter and the length/count of the list etc.

    Also, when posting code, please format it so there is not all the white space in front of the code, take time so that we can easily read your code as currently I needed to copy and paste the code into Notepad++.


    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.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    quarta-feira, 19 de agosto de 2020 14:18