-3

How to add values to a collection object in c#?

I am trying to add list of values which are in the incremental sequence to a collection object.

Sample syntax :-

    Source Data

FirstName   LastName    Department  Salary
-------------------------------------------------------
John        Boone       Developer   100
Albert      Post        Manager     500
Benjamin    Nayyar      Developer   100


public class MyGroup
{
        public int RowIndex { get; set; } // Row Index
        public int ColumnIndex { get; set; } // Column Index
        public object ColumnValue { get; set; } // Column Value
}


class A
{
List<MyGroup> objMyGrp = new List<MyGroup>();
int iGrpCount = 0; 

 objMyGrp[iGrpCount].RowIndex = excelCell.RowIndex;
 objMyGrp[iGrpCount].ColumnIndex = excelCell.ColumnIndex;
 objMyGrp[iGrpCount].ColumnValue = excelCell.Value.ToString();

}

iGrpCount++;





Expected output :-
RowIndex   ColumnIndex  ColumnValue
1       1   John
1       2   Boone
1       3   Developer
1       4   100 
2       1   Albert
2       2   Post
2       3   Manager
2       4   500
3       1   Benjamin
3       2   Nayyar  
3       3   Developer
3       4   100 

Error :- Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Expected Result :

Object objMGrp is a collection object which will store the Row Index Number, Column Index Number and Column value in the incremental sequence. I will have to use that object for sorting and validating the values.

goofyui
  • 3,362
  • 20
  • 72
  • 128
  • @steve .. please excuse about the actual code. Please ignore my last syntax part. how to add values which are iterated from foreach loop to this collection object ? – goofyui Jun 16 '19 at 21:26
  • @mjwillis .. syntax mentioned in the duplicate posting wasn't helpful... . Those syntax is designed for specific static values. When we load dynamic values, those syntax overwrites .. and does not load it in the indexing order – goofyui Jun 17 '19 at 00:52
  • https://stackoverflow.com/a/52588564/34092 is **almost exactly** the same thing as you need to do. If you disagree it is appropriate, please show your attempt to implement that idea and I'll show you what bit you missed. – mjwills Jun 17 '19 at 02:11
  • @mjwills , please forgive me. I am not disagreeing. Challenge is that , specific syntax mentioned is for static values. When we specifically define the values to the variables, we can make use of that syntax. However, when we iterate dataset through foreach loop and assign the values to the collection object , on each iteration .. present value over writes the existing value. Expected result objCollection[0] , objCollection[1] values has to be assigned to the variables as such – goofyui Jun 17 '19 at 15:03
  • Please show your attempt to implement that idea and I'll show you what bit you missed. There is no point discussing whether the idea works if you refuse to try it. I **know** it works. – mjwills Jun 17 '19 at 22:53

1 Answers1

0

If you have only declared a collection and created the instance you cannot use the index 0 on that collection. There are no elements so zero is outside the allowed range.

First you need to add an element to that collection

public class A
{
    // Private internal variables of this class
    List<MyGroup> objMyGrp = new List<MyGroup>();
    int iGrpCount = 0; 

    // This method adds a MyGroup instance to the collection
    public void SomeMethod()
    {
         // Some default values. You can pass parameters to this method
         // and then use parameters to initialize the MyGroup instance
         MyGroup grp = new MyGroup();
         grp.RowIndex = 0;
         grp.ColumnIndex = 0;
         grp.ObjectValue = null;

         objMyGrp.Add(grp);
         iGrpCount++;
    }

    public void SomeOtherMethod(int grpIndex)
    {
       // Now you could try to change any MyGroup instance stored in the 
       // collection to your desidered rowIndex
       if(grpIndex < iGrpCount)
          objMyGrp[grpIndex].RowIndex = 2;
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • it makes sense.. let me work on that .. thanks for your advice – goofyui Jun 16 '19 at 21:37
  • thank you.. but there is an issue.. we are not referring the index counter variable iGrpCount to the grp.RowIndex syntax. What it happens is that, every time we run through the loop the value is getting updated. – goofyui Jun 17 '19 at 00:46
  • 1
    I am sorry, but given you actual code I cannot understand what do you mean with _we run throug the loop the value is getting updated_ I have just explained what you should do to add an element to the collection as you have asked originally – Steve Jun 17 '19 at 06:43
  • Thank you for your help.. But .. what happens.. , given syntax is valid only for static values. When we want to assign the values dynamically by assigning through a foreach loop counter, new set of values are overwrites the existing assigned values – goofyui Jun 17 '19 at 13:32
  • i added with sample source data and expected output – goofyui Jun 17 '19 at 16:21