简体   繁体   中英

How to pass the List<String> value as String to the Other function

I have a List<> as lsSplitMember I am sending it through the while loop to the function as a string.But it throws exception message " Index was out of range . Must be non-negative and less than the size of the collection". I have tried with the below code .

//Globally declared variable lsSplitMember
List<String> lsSplitMember=new List<String>();



int ic = lsSplitMember.Count();
while (ic != 0)
{
  Process_Split(lsSplitMember[ic]);
  ic--;
}


Protected void Process_Split(String Member)
{
 //Some Code
}

So how can I Solve this problem?

int ic = lsSplitMember.Count();
while (ic != 0)
{
  Process_Split(lsSplitMember[ic-1]); // your index was off-by-one
  ic--;
}

Please note that using the C# language features, that's a lot of unnecessary meta-code:

foreach(var text in lsSplitMember)
{
  Process_Split(text);
}

This is a lot easier to read and way less error prone to write. If you need the list to be processes upside down, you can reverse it first.

The count is one greater than the last index of the List since they are zero-indexed, and this is the index you try to access. Really, you should use an iterator for this:

foreach( string s in lsSplitMember )
    Process_Split( s );

Try this...

Change Process_Split(lsSplitMember[ic]); to Process_Split(lsSplitMember[ic-1]);

Try this

List<String> lsSplitMember=new List<String>();



int ic = lsSplitMember.Count();
while (ic != 0)
{
  ic--;
  Process_Split(lsSplitMember[ic]);

}


Protected void Process_Split(String Member)
{
 //Some Code
}
lsSplitMember.ForEach(s =>
{
    Process_Split(s);
});

Provided lsSplitMember is not null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM