Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello ALL,

I have created a method which return a IList,

List return a one row,
So ,I want to set the list value in different label...

Here is my code..
C#
if (mediationinfolist.Count >= 1)
       {
           mediationinfo.Mediationinfolist = mediationinfolist;
       }

       for (int i = 1; i <= mediationinfo.Mediationinfolist.Count; i++)
       {
           lblMediationDate.Text = Functions.GetDate(mediationinfo.Mediationinfolist[i].MediationDate, 7);


}

it show index out of range exception.

please give me solution..
Posted
Updated 11-Dec-11 19:30pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Dec-11 1:24am    
A broken debugger or what?
--SA

Run your loop as
for (int i = 0; i <= mediationinfo.Mediationinfolist.Count-1; i++).

In the future, try and debug your code and I'm sure you will be able to find the solution yourself.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Dec-11 1:23am    
More standard pattern is for (int index = 0, index < count, ++index), it is even faster, to my surprise, I tested. Nevertheless, my 5 for a catch and especially for mentioning a debugger.
--SA
Abhinav S 12-Dec-11 2:11am    
Good point.
koolprasad2003 12-Dec-11 1:29am    
Yes. counter should be start with 0. from me 5.
Sergey Alexandrovich Kryukov 12-Dec-11 1:32am    
Please see the improved answer.
--SA
Abhinav S 12-Dec-11 2:11am    
Thank you.
Note following points about Array, list and index values
1. Index start with always 0 not 1.
2. Srart counter with 0 and loop until count-1

Read theory part first and then go for coding this is basic thing.
Happy programming.
 
Share this answer
 
Comments
Amir Mahfoozi 12-Dec-11 1:31am    
+5
Abhinav S 12-Dec-11 2:12am    
5!
Following the advice by one other expert, I tried to change i++ to ++i and, to my surprise, found that it works considerably faster (for small footprint in the loop body, of course). Also, some say the '<' condition is faster. I'll also improve naming; one-character names are really bad. So to improve the solution shown my Abhinav (which I credit as correct), do:


C#
for (int index = 0; index < mediationInfo.MediationInfoList.Count; index++) {/*...*/}


Use the debugger!

Good luck,
—SA
 
Share this answer
 
Comments
Amir Mahfoozi 12-Dec-11 1:35am    
+5 also he can use foreach loop.
Sergey Alexandrovich Kryukov 12-Dec-11 1:44am    
You are absolutely right, but if one needs to use index in the loop body, "for" might be more convenient, even though the index can be calculated with "foreach" as well, as an external stack variable, right?

OP does use the loop variable "i", that's why I stay with "for" in this case.

Thank you, Amir.
--SA
Amir Mahfoozi 12-Dec-11 1:48am    
There are some sample using of index in foreach loop , here : http://stackoverflow.com/questions/43021/c-get-index-of-current-foreach-iteration
I should confess that I didn't aware of those :-?
Sergey Alexandrovich Kryukov 12-Dec-11 2:24am    
Sure, good point. Note that the expert who explained the technique also prefers "for" when an index is needed, for a good reason.
--SA
madhuri@mumbai 12-Dec-11 2:08am    
Thanks all of you,
I got the answer..
its works fine...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900