4 Replies - 1119 Views - Last Post: 26 June 2013 - 06:51 AM Rate Topic: -----

#1 fanglign   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 48
  • Joined: 24-June 13

Array: Index was outside the bounds of the array.

Posted 25 June 2013 - 06:38 PM

Dim i, j As Integer
        Dim curModuleCode, modulecode() As String
        ReDim Preserve modulecode(i)
        Dim objModuleCode As Object

        DataGridView2.ColumnCount = 5
        DataGridView2.Columns(0).Name = "School"
        DataGridView2.Columns(1).Name = "Campus"
        DataGridView2.Columns(2).Name = "AdminNo"
        DataGridView2.Columns(3).Name = "ModuleCode"
        DataGridView2.Columns(4).Name = "ModuleGrp"


        j = 0
        For Each line As String In System.IO.File.ReadAllLines("J:\FYP\SEG1.txt")

            objModuleCode = line.Split(vbTab)
            curModuleCode = objModuleCode(3)
            'DataGridView2.Rows.Add(line.Split(";"))
            'Dim i = DataGridView2.Rows.Count

            'Check whether there is duplicates
            For i = 0 To j
                If modulecode(i) = curModuleCode Then

                    Exit For
                End If
            Next

            'The current module code is not duplicate
            If i = j + 1 Then
                'modulecode(i)

                DataGridView2.Rows.Add(line.Split(vbTab))
                modulecode(i) = curModuleCode
                'ReDim Preserve modulecode(j)
            End If


        Next



Hi, can anyone tell me whats wrong with this code? my "ModuleCode(i)=curModuleCode" is said to be index outside bounds of array.
i did not declare the size of the array because i dont know how many lines my data will be, so im using dynamic array.
Thanks in advance!

This post has been edited by modi123_1: 25 June 2013 - 06:49 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Array: Index was outside the bounds of the array.

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,781
  • Joined: 12-June 08

Re: Array: Index was outside the bounds of the array.

Posted 25 June 2013 - 06:52 PM

Put a breakpoint on that line and _see_ what that object is.

Ultiamtely you are going about arrays all wrong. If yo udo not know how many rows you will need then perhaps use a different collection - like 'list(of'.

AT the top you declare i.. and then use that as the size of your array in line 03.. if I was needing to be reset (or your need a bigger array) why are you not tellig your code to do that?
Was This Post Helpful? 0
  • +
  • -

#3 fanglign   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 48
  • Joined: 24-June 13

Re: Array: Index was outside the bounds of the array.

Posted 25 June 2013 - 11:12 PM

View Postmodi123_1, on 25 June 2013 - 06:52 PM, said:

Put a breakpoint on that line and _see_ what that object is.

Ultiamtely you are going about arrays all wrong. If yo udo not know how many rows you will need then perhaps use a different collection - like 'list(of'.

AT the top you declare i.. and then use that as the size of your array in line 03.. if I was needing to be reset (or your need a bigger array) why are you not tellig your code to do that?


i dont understand what you mean. sorry, but im a very beginner in VB. having problems with array.
intially i declare module code with a size and a file with fewer datas, it worked in getting rid of the duplicates
after i changed the file with more records, declaring size of array more than emough, it doesnt get rid of the duplicates.
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Array: Index was outside the bounds of the array.

Posted 26 June 2013 - 06:45 AM

View Postfanglign, on 26 June 2013 - 12:12 AM, said:

i dont understand what you mean. sorry, but im a very beginner in VB. having problems with array.
intially i declare module code with a size and a file with fewer datas, it worked in getting rid of the duplicates
after i changed the file with more records, declaring size of array more than emough, it doesnt get rid of the duplicates.

Initially, you Dim i and j as Integers.
Two lines later, you ReDim an array with i. Ask yourself a question. "What is the value of the variable i?" If you don't know, have a look at the documentation for Dim, as it applies to numeric vaues.

Have a look at one of our Debugging Tutorials, here or here to find out how to set breakpoints and look at the values in variables.

Set Option Strict On. See the link in my signature to find out why.

modi123_1 gave you good advice about using a List(Of T) instead of an array. They can be added to without a ReDim, but can still be accessed with an index.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,781
  • Joined: 12-June 08

Re: Array: Index was outside the bounds of the array.

Posted 26 June 2013 - 06:51 AM

Read this: Debugging

As I said you declare the variable 'i', but give it it no value. So it's 0.
01	Dim i, j As Integer



You then use the variable 'i' as a size for an array...
03	        ReDim Preserve modulecode(i)



Here I have no idea what you think you are doing..
24	                If modulecode(i) = curModuleCode Then



Alternatively you can use a "list(of" and just 'add' to the collection.. no need to guess what you think this array size should be.

Example:

        Dim foo As New List(Of String)

        foo.Add("asdf")

        foo.Add("1235a")

        foo.Add("7894sdf")


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1