6 Replies - 772 Views - Last Post: 10 March 2013 - 07:37 AM Rate Topic: -----

#1 HazXoD3D  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 04-December 09

[Problem] Index of outside of the array?!

Posted 08 March 2013 - 07:49 AM

Im Started a new small storageprogram today. But ive been stuck at a point where i always gets the error "Index is outside the bounds of the array".

What works is: to recive the files from localdisk and create a List of all the files/blocks with identity, website...etc

But the error occours at when im calling the function CreateBlock from CreateSheet


public partial class frmMain : Form
    {
        lckdDatabase Database;

        public frmMain()
        {
            InitializeComponent();

            Database = new lckdDatabase(@"C:\Database\");
            
            if (Database.Completed)
                CreateSheet();
        }

        private void CreateSheet()
        {
            ListViewItem[] lsvItems = Database.GetDatabase();

            foreach (ListViewItem lsv in lsvItems)
            {
                lsvDatabase.Items.Add(lsv);
            }
        }
    }

    public class lckdDatabase
    {
        public List<lckdBlock> Blocks;
        private string strDirectory = string.Empty;

        public bool Completed;

        public lckdDatabase(string Directory)
        {
            strDirectory = Directory;

            Blocks = new List<lckdBlock> { };
            LoadDatabase();
        }

        public void LoadDatabase()
        {
            try
            {
                string[] strPaths = Directory.GetFiles(strDirectory, "*.lckd", SearchOption.TopDirectoryOnly);

                for (int x = 0; x < strPaths.Length; x++)
                {
                    string[] arrInfo = File.ReadAllText(strPaths[x], System.Text.Encoding.UTF8).Split('|');

                    lckdBlock Block = new lckdBlock(new FileInfo(strPaths[x]).Name, arrInfo);
                    Blocks.Add(Block);
                }
            }
            catch (Exception ex)
            {
                Completed = false;
            }
            finally
            {
                Completed = true;
            }
        }

        public void CreateBlock(string[] arrBlock )
        {
            File.Create(strDirectory + arrBlock[0] + ".lckd");

            LoadDatabase();
        }

        public ListViewItem[] GetDatabase()
        {
            ListViewItem[] Items = new ListViewItem[]{};

            for(int x = 0; x < Blocks.Count; x++)
            {
                Items[x].Tag = Blocks[x].Identity;
                Items[x].Text = Blocks[x].Website;
                Items[x].SubItems[0].Text = Blocks[x].Username;
                Items[x].SubItems[1].Text = Blocks[x].Password;
                Items[x].SubItems[2].Text = Blocks[x].Created;
            }

            return Items;
        }
    }

    public class lckdBlock
    {
        public lckdBlock(string Identity, string[] Blockspecification)
        {
            this.Identity   = Identity;
            Website         = Blockspecification[0];
            Username        = Blockspecification[1];
            Password        = Blockspecification[2];
            Created         = Blockspecification[3];
        }

        private string _Identity;
        public string Identity 
        {
            get { return _Identity; }
            set { _Identity = value; }
        }

        private string _Website;
        public string Website
        {
            get { return _Website; }
            set { _Website = value; }
        }

        private string _Username;
        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        private string _Password;
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        private string _Created;
        public string Created
        {
            get { return _Created; }
            set { _Created = value; }
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: [Problem] Index of outside of the array?!

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5437
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: [Problem] Index of outside of the array?!

Posted 08 March 2013 - 07:58 AM

Arrays are zero-indexed. 10 items are indexes 0-9

If you try to loop to index 10, you are out of bounds because that's the 11th item.




tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 HazXoD3D  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 04-December 09

Re: [Problem] Index of outside of the array?!

Posted 08 March 2013 - 08:11 AM

View PosttlhIn`toq, on 08 March 2013 - 07:58 AM, said:

Arrays are zero-indexed. 10 items are indexes 0-9

If you try to loop to index 10, you are out of bounds because that's the 11th item.


I have used Breakpoint and there is index 0, 1 and 2 in the list(Blocks).
The error occours on eighter 0, 1 and 2, if I switch in the aboved loop.

So it should not be out of bounds:S

This post has been edited by HazXoD3D: 08 March 2013 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: [Problem] Index of outside of the array?!

Posted 08 March 2013 - 08:27 AM

Where do you call CreateBlock as I can spot it.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5437
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: [Problem] Index of outside of the array?!

Posted 08 March 2013 - 08:29 AM

You just have to keep following execution, one line at a time until the error gets thrown.
Its boring and time consumming. Welcome to debugging.

Something is probably happening to change the size of the collection while you are working with it. For example, when you make MakeBlock() you also LoadDatabase(). Maybe that is resetting the listview... thus by the time you are on your second or third loop iteration there is no longer a second or third item.
Was This Post Helpful? 2
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5437
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: [Problem] Index of outside of the array?!

Posted 08 March 2013 - 08:43 AM

the loop at line 76 has issues.
You never make a new ListViewItem to put into the index.
YOu make an array (which is EMPTY at creation) then start interacting with the indexes of the array (that don't yet exist), without ever populating the array with ListViewItems.

You see it here in this simple example
Notice how the array has a size of [0] items.

Posted Image

You should probably work some tutorials introducing you to arrays before trying to create an entire application that is depending on them.

This post has been edited by tlhIn`toq: 08 March 2013 - 08:41 AM

Was This Post Helpful? 1
  • +
  • -

#7 HazXoD3D  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 04-December 09

Re: [Problem] Index of outside of the array?!

Posted 10 March 2013 - 07:37 AM

View PosttlhIn`toq, on 08 March 2013 - 08:29 AM, said:

You just have to keep following execution, one line at a time until the error gets thrown.
Its boring and time consumming. Welcome to debugging.

Something is probably happening to change the size of the collection while you are working with it. For example, when you make MakeBlock() you also LoadDatabase(). Maybe that is resetting the listview... thus by the time you are on your second or third loop iteration there is no longer a second or third item.



View PosttlhIn`toq, on 08 March 2013 - 08:43 AM, said:

the loop at line 76 has issues.
You never make a new ListViewItem to put into the index.
YOu make an array (which is EMPTY at creation) then start interacting with the indexes of the array (that don't yet exist), without ever populating the array with ListViewItems.

You see it here in this simple example
Notice how the array has a size of [0] items.

Posted Image

You should probably work some tutorials introducing you to arrays before trying to create an entire application that is depending on them.



thanks all of you for your time explaining.


I understand the issue now after some google searches :)

I read some different from a List and an Array:
A List is "Dynamic" and can be resized under debugging, while Arrays is "static" and has the initial state of size alltime.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1