0

I am looping through a list and would like to add multiple occurrences, should I find them.

so far I have,

public struct optionsSort
    {

        public string name;
        public string date;
        public double strike;
        public string callPut;
        public double size;
        public string isin;
    }




    List<List<optionsSort>> stocks = new List<List<optionsSort>>();
    optionsSort tempStock1 = new optionsSort();
    List<optionsSort> posCheckOptions = new List<optionsSort>();

then some code, then,

 for(int k = 0; k<posCheckOptions.Count; k++)
        {
            for(int l = 0; l<posCheckOptions[l].Count; l++)
            {
                if(posCheckOptions[l+1] == null)
                {
                    //finished the iteration
                    break;
                }
                else if
                (posCheckOptions[k][l + 1].date == posCheckOptions[k][l].date 
                    && posCheckOptions[k][l + 1].strike == posCheckOptions[k][l].strike
                    && posCheckOptions[k][l + 1].callPut == posCheckOptions[k][l].callPut)
                {

                    posCheckOptions[k][l].size = posCheckOptions[k][l].size 
                                                 + posCheckOptions[k][l + 1].size;



                }

            }

        }

Basicly, Im looking forward from the start of the list. asking the question, are certain elements of the list at i+1 the same as i, if so, add those elements to i and delete the entire row.

i get this error

"Error 1 Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable C:\Users\WindowsFormsApplication1\WindowsFormsApplication1\ReadCSV.cs 890 25 WindowsFormsApplication1 "

Many Thanks for looking.

Timujin
  • 171
  • 1
  • 4
  • 14
  • 1
    We don't know the type of `posCheckOptions` which makes it hard to help you. Presumably it's a mutable struct (urgh) - either make it a mutable class, or change your code to fetch the value to a local variable, modify it, then store it back in the list. I strongly suspect this code could be generally made simpler, mind you... – Jon Skeet Jul 20 '13 at 16:59
  • public struct optionsSort { public string name; public string date; public double strike; public string callPut; public double size; public string isin; } List> stocks = new List>(); optionsSort tempStock1 = new optionsSort(); List posCheckOptions = new List(); – Timujin Jul 20 '13 at 17:02
  • Please edit this into the question. Personally I don't like either mutable structs or public fields, but that's your call... – Jon Skeet Jul 20 '13 at 17:03
  • in your for loop,why not the stocks list?...i guess you add each posCheckOptions to your stocks. – terrybozzio Jul 20 '13 at 17:06
  • In your second `for` loop condition, I think you meant `posCheckOptions[k]`, not `posCheckOptions[l]`. – Kendall Frey Jul 20 '13 at 17:08
  • What line is the error occurring on? – Kendall Frey Jul 20 '13 at 17:09
  • Thats earlier in the code. the list posCheckOptions is populated. has all elements of my positions per stock. and per stock, some positions can be added.. so rather than having 50 elements at posCheckOtions[0] if i add them i would maybe have 45 for example – Timujin Jul 20 '13 at 17:09
  • @kendallFrey posCheckOptions[k][l].size = posCheckOptions[k][l].size + posCheckOptions[k][l + 1].size; – Timujin Jul 20 '13 at 17:12
  • I noticed that you have never accepted an answer on one of your questions. If an answer answers your question, you should select the checkbox beside it to show that your question has been answered. – Kendall Frey Jul 20 '13 at 17:26

1 Answers1

2

I believe your problem is that you are using a mutable struct. Mutable structs are evil.

The simplest solution is to change optionsSort to a class. That should fix the error message.


To explain the error message, when you call posCheckOptions[k][l], since optionsSort is a struct, it returns a copy of the value in the list. When you change size, it will update the copy, but not the one in the list. The copy would then be discarded. The compiler recognizes this and stops you.


I recommend you read up on the differences between reference types and value types.

Community
  • 1
  • 1
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • is it just that simple to do, public struct to public class? – Timujin Jul 20 '13 at 17:15
  • mmmn, something not right, i have to change all my structs to class? when I do, as i step through parts of the code, so if statments are not triggering, they were when I using structs... – Timujin Jul 20 '13 at 17:30