9 Replies - 427 Views - Last Post: 25 July 2013 - 01:19 PM Rate Topic: -----

#1 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Index out of range ?

Posted 25 July 2013 - 06:22 AM

Hi,

I making some photo viewer app, so in this class i list all pictures from main app folder at begin program, and from user directories who is opened using openfiledialog, then when user press ok on ofd, i send directory path to properties class PropFields, and set IsFileOpen to true.

Then i check in this class is file open, if is, then i'm trying to filter directory path files, and check is that file picture, i check that using LINQ:

var filter = Directory.GetFiles(path).Where(p => p.ToUpper().EndsWith(ext));


after that i convert var to list, then count list values and send it to PropFields.Max, i use max in main class as limit number of pic's.

then i in string file get value of selected picture, counter has start value as 0, if i press next button it's increment by 1, or if i press previous button it's decremented by 1.


I don't know what is problem here, but i get Exception from third catch block from this code here where is Index out of range?

How is that possible because i convert var to list?

When i start application, i get error and then pic show's normally, then i press next, again error and net pic shows normally, then i go to open some directory where are diff file types, some no pic files which is not in my list, and then i can't list pic's normally, just first pic shows.


Here is code.
private List<string> extensions = new List<string>
        {
                ".JPG",
                ".JPEG",
                ".PNG",
                ".BMP",
                ".ICO",
                ".GIF",
                ".TIFF",
                ".WMF"
        };

        public void GetImage(int counter)
        {   
            string path = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString() + @"\images\";

            //if openfiledialog returns true
            if (PropFields.IsFileOpen)
            {
                path = Path.GetDirectoryName(PropFields.Path); //new path
            } 


            try
            {
                List<string> lis = new List<string>();
                foreach (string ext in extensions) //list extenstions
                {
                    var filter = Directory.GetFiles(path).Where(p => p.ToUpper().EndsWith(ext));
                    //filter just pic files

                    lis = filter.ToList();


                    int max = lis.Count(); //max
                    string file = lis[counter].ToString(); //file is equal selected pic path filtered up

                    PropFields.Max = max; 
                    PropFields.Path = file;
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



Sorry for bad English, Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Index out of range ?

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Index out of range ?

Posted 25 July 2013 - 06:30 AM

C# arrays start at 0. Count and Length always are n+1. Subtract 1 from your lis[counter] and it should work.
Was This Post Helpful? 0
  • +
  • -

#3 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Index out of range ?

Posted 25 July 2013 - 06:48 AM

View Postsnoj, on 25 July 2013 - 06:30 AM, said:

C# arrays start at 0. Count and Length always are n+1. Subtract 1 from your lis[counter] and it should work.


You mean subtract like here:

string file = lis[counter-1].ToString();

doesn't work.

This post has been edited by DocNet: 25 July 2013 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,513
  • Joined: 08-April 09

Re: Index out of range ?

Posted 25 July 2013 - 07:27 AM

Please be more specific, "doesn't work" is not good, what are you getting and what is expected.
Was This Post Helpful? 0
  • +
  • -

#5 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Index out of range ?

Posted 25 July 2013 - 07:38 AM

Whoops, I misread the code, I thought I had read that counter was set in the loop.

At any rate, whatever the value of counter is, it's outside the range of lis. For instance, if lis.Count() == 0, any value of counter is invalid. You may want to check if counter is less than lis.Count() or just eat the exception and move on with whatever is calling GetImage().
Was This Post Helpful? 2
  • +
  • -

#6 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Index out of range ?

Posted 25 July 2013 - 08:19 AM

View Postsnoj, on 25 July 2013 - 07:38 AM, said:

Whoops, I misread the code, I thought I had read that counter was set in the loop.

At any rate, whatever the value of counter is, it's outside the range of lis. For instance, if lis.Count() == 0, any value of counter is invalid. You may want to check if counter is less than lis.Count() or just eat the exception and move on with whatever is calling GetImage().


Excellent :^:
Now works.

correct code:
if (counter < lis.Count)
                    {
                        int max = lis.Count(); //max
                        string file = lis[counter].ToString(); //file is equal selected pic path


                        PropFields.Max = max;
                        PropFields.Path = file;
                    }


Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Index out of range ?

Posted 25 July 2013 - 08:25 AM

Make sure you are also checking that counter >= 0 as what happens when the person hits the previous button when they are on the first picture (or even better, disable the previous button when counter = 0 and disable next when counter = last picture, but that would require you to rethink your logic as you don't know the size of the list until you are trying to display the picture. Personally I'd have the list created when the select the directory, not every time they ask for a picture).
Was This Post Helpful? 0
  • +
  • -

#8 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Index out of range ?

Posted 25 July 2013 - 09:39 AM

View PostMomerath, on 25 July 2013 - 08:25 AM, said:

Make sure you are also checking that counter >= 0 as what happens when the person hits the previous button when they are on the first picture (or even better, disable the previous button when counter = 0 and disable next when counter = last picture, but that would require you to rethink your logic as you don't know the size of the list until you are trying to display the picture. Personally I'd have the list created when the select the directory, not every time they ask for a picture).


Yes, i have now that problem.

I don't want to disable button's, it's much better when i rotate, when is counter on max, turn back to 0

here is logic how that work...

it's work before counter checking, here is code:
private void NextPic()
        {
            if (counter < PropFields.Max)
            {
                counter++;
            }
            else
            {
                counter = -1;
                counter++;//0
            }
            RenderImage();
        }

        private void PrevPic()
        {
            if (counter <= PropFields.Max && counter > 0) //5
            {
                counter--; 
            }
            else
            {
                counter = PropFields.Max+1;
                counter--;
            }
            RenderImage();
        }


Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Index out of range ?

Posted 25 July 2013 - 01:01 PM

Why set it to a value just to change that value by 1 in the next line? Just set it directly and use modulus arithmetic:
private void NextPic() {
    counter++;
    counter %= PropFields.Max;
    RenderImage();
}

private void PrevPic() {
    counter--;
    counter %= PropFields.Max;
    RenderImage();
}  

This post has been edited by Momerath: 25 July 2013 - 01:01 PM
Reason for edit:: Some day I will learn to spell and not make typos

Was This Post Helpful? 0
  • +
  • -

#10 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Index out of range ?

Posted 25 July 2013 - 01:19 PM

Don't forget about when counter == Propfields.Max or you'll go out of bounds again!

counter = (counter == Profields.Max) ? 0 : counter;



I don't apparently know what I'm talking about.

This post has been edited by snoj: 25 July 2013 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1