Page 1 of 2 12 LastLast
Results 1 to 40 of 47

Thread: [RESOLVED] [Index was outside the bounds of the array.] But it's not

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Resolved [RESOLVED] [Index was outside the bounds of the array.] But it's not

    I keep getting a [Index was outside the bounds of the array.] for a Text Bool to CBool conversion. I have a 2D array, 27 Row, 3 Col. The call is to the row of 2 and the column of 2. See below. Yet the program keeps jumping to the catch, and I have a boolean variable that is used just before the first catch error and the result = the spreadsheet setting. Yet...... It errors. This just started and it was working fine with taking the Bool from the spreadsheet and place it in the checkbox selection. See below.

    Not sure what could be happening, as this is strange. Below are all of the code I believe is needed for demonstration of the issue. Please what could be causing this unnormal event?

    Loading the checkboxes options default settings, where the error occurs.
    Code:
                Dim x As Boolean = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_CLOSE, cCOL_START))
                CK_AUTO_CLOSE_MAIN.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_CLOSE, cCOL_START))
                CK_AUTO_ASSIGN_MAIN.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_ASSIGN, cCOL_START))
                CK_AUTO_RELAUNCH_MAIN.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_RELAUNCH, cCOL_START))
                CK_USE_CHECKLIST.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_GENERAL_USE_CHECKLIST, cCOL_START))
                CK_AUTO_GEN_CHECKLIST.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_TASK_AUTO_CREATE_CHECKLIST, cCOL_START))
                CK_AUTO_CREATE_PER_FOLDER.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_TASK_AUTO_CREATE_PER_FOLDER, cCOL_START))
                CK_AUTO_LAUNCH_PI.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_GENERAL_AUTO_LAUNCH_PXX, cCOL_START))
                CK_AUTO_LOAD_PI_LIST.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_GENERAL_AUTO_LOAD_PI_LIST, cCOL_START))
                CK_AUTO_LAUNCH_RISK.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_GENERAL_AUTO_LAUNCH_RISK, cCOL_START))
                CK_AUTO_OPEN_ATTACHMENTS.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_REVIEW_AUTO_OPEN_ATTACHMENTS, cCOL_START))
                CK_AUTO_COMPARE_CHECKLIST.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_REVIEW_AUTO_COMPARE_CHECKLIST, cCOL_START))
                CK_AUTO_RUN.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_GENERAL_AUTO_RUN, cCOL_START))
    Spreadsheet data
    Code:
    Default Option	DO Setting	Comment
    Main Auto Close	TRUE	Closed the Tab if it has a "Close" button (After Updates)
    Main Auto Assign	TRUE	Re-assigns the Main to the user
    Main Spare 1	FALSE	Spare bit
    Main Auto Relaunch	TRUE	Reopens the Main when the Main is Closed automatically
    Assign Spare 1	FALSE	
    Task Auto Create Checklist	FALSE	Are we going to use a checklist in this TASK
    Task Create PER Folder	TRUE	Creates the PER folder if needed
    General Auto Run	FALSE	Let the program run indefinatly by it's self
    General Auto Load PI List	TRUE	Automatically load the users PI list
    General Auto Launch PI	FALSE	Automatically launch the next selected PI
    General Auto Launch Risk Analysis	FALSE	Will automatically launch the risk check when the main is loaded
    General Spare 2	FALSE	
    General Spare 3	FALSE	
    General Spare 4	FALSE	
    General Generate Checklist	FALSE	User is using a checklist
    General Auto Open Attachments	TRUE	Automatically open any attachments that the PIT may have
    Review Auto Compare Checklist	TRUE	Compare the checklist with the PER information
    Application Running	0	1 = Running; This is set to 1 at start. Prevent restart since it will be 1 on the next false start (DOES NOT WORK)
    Country United States	TRUE	United States
    Country China	TRUE	China
    Country Austrailia	TRUE	Austrailia
    Country Germany	FALSE	Germany
    Country Japan	TRUE	Japan
    Country United Kindom	TRUE	United Kindom
    Country Brazil	FALSE	Brazil
    Country Sweden	TRUE	Sweden
    Values in some of the array.

    Code:
    		(1, 1)	"Default Option"	Object {String}
    		(1, 2)	"DO Setting"	Object {String}
    		(1, 3)	"Comment"	Object {String}
    		(2, 1)	"Main Auto Close"	Object {String}
    		(2, 2)	"True"	Object {String}
    		(2, 3)	"Closed the Tab if it has a ""Close"" button (After Updates)"	Object {String}
    		(3, 1)	"Main Auto Assign"	Object {String}
    		(3, 2)	"True"	Object {String}
    		(3, 3)	"Re-assigns the Main to the user"	Object {String}
    		(4, 1)	"Main Spare 1"	Object {String}
    		(4, 2)	False	Object {Boolean}
    		(4, 3)	"Spare bit"	Object {String}
    This is in the Load section of the main Form, and is before the GUI is displayed.

    I can let it "Error" and then reset the run location back to the error location, and It will run that again without fail. However, the next item to be called fails, and so on wash, rinse, repeat.

    EDIT: Thought someone might ask for the ENUM section. See below.

    Code:
        '--------------------------------------------------------------------
        ' Enum: Options
        '--------------------------------------------------------------------
        Public Enum ENUM_OPTIONS
            'Main
            eROW_OPTIONS_MAIN_AUTO_CLOSE = 2
            eROW_OPTIONS_MAIN_AUTO_ASSIGN
            eROW_OPTIONS_MAIN_SPARE_1
            eROW_OPTIONS_MAIN_AUTO_RELAUNCH
            'Assign
            eROW_OPTIONS_ASSIGN_SPARE_1
            'Task
            eROW_OPTIONS_TASK_AUTO_CREATE_CHECKLIST
            eROW_OPTIONS_TASK_AUTO_CREATE_PER_FOLDER
            'General
            eROW_OPTIONS_GENERAL_AUTO_RUN
            eROW_OPTIONS_GENERAL_AUTO_LOAD_PI_LIST
            eROW_OPTIONS_GENERAL_AUTO_LAUNCH_PXX
            eROW_OPTIONS_GENERAL_AUTO_LAUNCH_RISK
            eROW_OPTIONS_GENERAL_MANUALLY_SPARE_2
            eROW_OPTIONS_GENERAL_SPARE_3
            eROW_OPTIONS_GENERAL_SPARE_4
            eROW_OPTIONS_GENERAL_USE_CHECKLIST
    
            'Review
            eROW_OPTIONS_REVIEW_AUTO_OPEN_ATTACHMENTS
            eROW_OPTIONS_REVIEW_AUTO_COMPARE_CHECKLIST
    
            eROW_OPTIONS_APPLICATION_RUNNING
    
            eROW_COUNTRIES_UNITED_STATES
            eROW_COUNTRIES_CHINA
            eROW_COUNTRIES_AUSTRAILIA
            eROW_COUNTRIES_GERMANY
            eROW_COUNTRIES_JAPAN
            eROW_COUNTRIES_UNITED_KINGDOM
            eROW_COUNTRIES_BRAZIL
            eROW_COUNTRIES_SWEDEN
    
        End Enum
    Last edited by FunkMonkey; Jun 5th, 2025 at 03:17 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,132

    Re: [Index was outside the bounds of the array.] But it's not

    It is very difficult to follow your business logic without some sort of minimal reproducible example. What I can suggest to you is to setup a breakpoint on the line that throws the exception then in your watch window see what the upper-bounds of the array are and compare them to the index that is trying to be accessed.

    My suspicion is that you're not loading your spreadsheet into the multi-dimensional array like you expect it to and making the assumption the data fits.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    You’re aware VB.Net arrays are zero based?
    A 2 element array contains an element at index 0 and an element at index 1

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by .paul. View Post
    You’re aware VB.Net arrays are zero based?
    A 2 element array contains an element at index 0 and an element at index 1
    It starts at (1), please see the capture of the array above. Also, to verify I just forced the Row to access Index (0) and I get an "Out of bounds" error message just to verify I was not missing something per your post.

    Any other suggestions.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    What line does it break on, and what are the EXACT index values at that time?
    Show the code it affects only…

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    The Code it breaks on is the first section above. I know that the return from the array is correct as I used a variable just before the break and it returned the correct boolean value. I have cut/paste a section of the code above for your convienence.

    Code:
    Dim x As Boolean = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_CLOSE, cCOL_START))
                CK_AUTO_CLOSE_MAIN.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_CLOSE, cCOL_START))
    The array information is also there in the original post but will repost it here.

    Code:
    (1, 1)	"Default Option"	Object {String}
    		(1, 2)	"DO Setting"	Object {String}
    		(1, 3)	"Comment"	Object {String}
    		(2, 1)	"Main Auto Close"	Object {String}
    		(2, 2)	"True"	Object {String}
    		(2, 3)	"Closed the Tab if it has a ""Close"" button (After Updates)"	Object {String}
    		(3, 1)	"Main Auto Assign"	Object {String}
    		(3, 2)	"True"	Object {String}
    		(3, 3)	"Re-assigns the Main to the user"	Object {String}
    		(4, 1)	"Main Spare 1"	Object {String}
    		(4, 2)	False	Object {Boolean}
    		(4, 3)	"Spare bit"	Object {String}
    The strange thing is that it was working fine. Not sure what I did to break it, but the checkboxes won't load from the array. I did hardcode them, and they loaded all false did not try TRUE though.

    Any thoughts?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    If VS tells you the index is outside of the bounds of the array, the chances are the index is outside of the bounds of the array.

    I've mentioned this before about your code, but you're showing indices as one of millions of hard to read constants. If you want to find the cause of the error, put a breakpoint in your code there, run your code and then step through your code and check each variable or constant involved.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,132

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by FunkMonkey View Post
    It starts at (1), please see the capture of the array above. Also, to verify I just forced the Row to access Index (0) and I get an "Out of bounds" error message just to verify I was not missing something per your post.

    Any other suggestions.
    If you're getting an out of bounds exception at index 0, then that means your array isn't populated at all.
    Last edited by dday9; Jun 6th, 2025 at 08:51 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,909

    Re: [Index was outside the bounds of the array.] But it's not

    In VB6, you could have 1 based arrays. You probably can in VBA, as well. In .NET, you have 0 based arrays and that is ALL you have. You can't alter that. If you think you are using 1-based arrays...you are mistaken. Of course, you can make a 0 based array and ignore the first element, which will look like a 1-based array, but it will have a different size.

    It's as DDay stated, though: If you are finding nothing in the 0 element, then the array is empty, because there is no such thing as a 1-based array in .NET.
    My usual boring signature: Nothing

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,836

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Shaggy Hiker View Post
    In .NET, you have 0 based arrays and that is ALL you have. You can't alter that.
    Actually, that's not true. You can create arrays in .Net with any lowerbound you desire:-
    Code:
    Dim my2DArr = Array.CreateInstance(GetType(String), {10, 10}, {1, 1})
    The above code creates an 2D Integer array with a lower bound of (1,1).

    I doubt this is what OP did though as very few .Net programmers are even aware you can do this.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Niya View Post
    Actually, that's not true. You can create arrays in .Net with any lowerbound you desire:-
    Code:
    Dim my2DArr = Array.CreateInstance(GetType(String), {10, 10}, {1, 1})
    The above code creates an 2D Integer array with a lower bound of (1,1).

    I doubt this is what OP did though as very few .Net programmers are even aware you can do this.
    I thought there was a way. I was aware of that method on Feb 12th, 2014 Question is, Are you really that obsessive about array LowerBounds? It's just another way of clinging on to classic VB, even if it is more work creating and using them...

    https://www.vbforums.com/showthread....=1#post4615349

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,836

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by .paul. View Post
    Are you really that obsessive about array LowerBounds?
    You'd be quite surprised. Linux vs Windows, VB6 vs VB.Net, iOS vs Android....those are nothing. Wait till you see an argument over 1-based vs 0-based indexing.....whoo boy

    I have no doubt that there are people out there who are willing die for 1-based indexing. They will gut you and leave you to die in the street if you even suggest to them that 0 based indexing is better(which it is )
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Niya View Post
    You'd be quite surprised. Linux vs Windows, VB6 vs VB.Net, iOS vs Android....those are nothing. Wait till you see an argument over 1-based vs 0-based indexing.....whoo boy

    I have no doubt that there are people out there who are willing die for 1-based indexing. They will gut you and leave you to die in the street if you even suggest to them that 0 based indexing is better(which it is )

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,909

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Niya View Post
    Actually, that's not true. You can create arrays in .Net with any lowerbound you desire:-
    Code:
    Dim my2DArr = Array.CreateInstance(GetType(String), {10, 10}, {1, 1})
    The above code creates an 2D Integer array with a lower bound of (1,1).

    I doubt this is what OP did though as very few .Net programmers are even aware you can do this.
    Well, I certainly didn't know that. In VB6 you had Option Base 1 (if I remember right), which may have been default.

    I understand the argument over 1 based arrays...I just don't care about it. It alters the math in ways that some people find convenient while others find inconvenient.
    My usual boring signature: Nothing

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,836

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Shaggy Hiker View Post
    Well, I certainly didn't know that. In VB6 you had Option Base 1 (if I remember right), which may have been default.
    Yes, Option Base.

    Quote Originally Posted by Shaggy Hiker View Post
    I understand the argument over 1 based arrays...I just don't care about it. It alters the math in ways that some people find convenient while others find inconvenient.
    Zero-based indexing is generally superior due to its mathematical simplicity and its alignment with how memory is addressed at the hardware level. The confusion often arises from the term "index", which implies a positional label rather than a memory offset. This framing leads to a flawed mental model when reasoning about arrays. In reality, an index represents the number of elements offset from the base address (i.e., the first element). Therefore, an index of 0 denotes a zero-offset, accessing the element located exactly at the base rather than implying a distinct "zeroth" element in a positional sense.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Niya View Post
    Yes, Option Base.



    Zero-based indexing is generally superior due to its mathematical simplicity and its alignment with how memory is addressed at the hardware level. The confusion often arises from the term "index", which implies a positional label rather than a memory offset. This framing leads to a flawed mental model when reasoning about arrays. In reality, an index represents the number of elements offset from the base address (i.e., the first element). Therefore, an index of 0 denotes a zero-offset, accessing the element located exactly at the base rather than implying a distinct "zeroth" element in a positional sense.
    Haven't been in this thread for a while, but I guess Microsoft's debugger must have a bug in it then because as seen below, the array starts at (1). this is a direct read from an Excel spreadsheet using Range. If it started at (0) based I would expect the array to start at (0) based. I know I have not programmed in C/C++ for a while now, but I would not think that Microsoft would change their debugger to start at index(1) when they mean index (0)?

    Code:
    (1, 1)	"Default Option"	Object {String}
    		(1, 2)	"DO Setting"	Object {String}
    		(1, 3)	"Comment"	Object {String}
    		(2, 1)	"Main Auto Close"	Object {String}
    		(2, 2)	"True"	Object {String}
    		(2, 3)	"Closed the Tab if it has a ""Close"" button (After Updates)"	Object {String}
    		(3, 1)	"Main Auto Assign"	Object {String}
    		(3, 2)	"True"	Object {String}
    		(3, 3)	"Re-assigns the Main to the user"	Object {String}
    		(4, 1)	"Main Spare 1"	Object {String}
    		(4, 2)	False	Object {Boolean}
    		(4, 3)	"Spare bit"	Object {String}

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,132

    Re: [Index was outside the bounds of the array.] But it's not

    Do me a favor and just give us the results of the following:
    Code:
    Console.WriteLine("Lower bounds of the first dimension: {0}", my2dArray.GetLowerBound(0))
    Console.WriteLine("Upper bounds of the first dimension: {0}", my2dArray.GetUpperBound(0))
    Console.WriteLine("Lower bounds of the second dimension: {0}", my2dArray.GetLowerBound(1))
    Console.WriteLine("Upper bounds of the second dimension: {0}", my2dArray.GetUpperBound(1))
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    You should do as dday asked, as it’s relevant to your problem…

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,909

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Niya View Post
    Zero-based indexing is generally superior due to its mathematical simplicity and its alignment with how memory is addressed at the hardware level. The confusion often arises from the term "index", which implies a positional label rather than a memory offset. This framing leads to a flawed mental model when reasoning about arrays. In reality, an index represents the number of elements offset from the base address (i.e., the first element). Therefore, an index of 0 denotes a zero-offset, accessing the element located exactly at the base rather than implying a distinct "zeroth" element in a positional sense.
    It is also worth noting that, "off by one" errors are so common in coding that they are the butt of jokes. When there is something that causes people to frequently make mental errors, then it's something worth thinking about.
    My usual boring signature: Nothing

  20. #20
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,869

    Re: [Index was outside the bounds of the array.] But it's not

    My New Year's resolution is to stop making off-by-one errors.
    So far, I'm off to a great start, having made only 0 errors this year!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  21. #21
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,553

    Re: [Index was outside the bounds of the array.] But it's not

    If the OP would post the code that actually reads the spreadsheet and populates the array that would probably help.

    That being said, the OP hasn't stated if this error is still occurring or if it has been fixed. An Index Out of Bounds error should be a trivial problem to track down and fix. Do the values of the indices used when the error occurs match with the expectation of what they should be? If not, why not? Is it possible that events are causing your code to try to access the array before it has been populated?

    Lastly, and this was touched on in another thread, but ENUM_PALOOZA makes it difficult to follow what code is doing. I'm not saying it is bad or good or that you need to change it, but for an outsider to look at it and offer help, it adds a level of complication that will likely make some people not bother trying to help.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by dday9 View Post
    Do me a favor and just give us the results of the following:
    Code:
    Console.WriteLine("Lower bounds of the first dimension: {0}", my2dArray.GetLowerBound(0))
    Console.WriteLine("Upper bounds of the first dimension: {0}", my2dArray.GetUpperBound(0))
    Console.WriteLine("Lower bounds of the second dimension: {0}", my2dArray.GetLowerBound(1))
    Console.WriteLine("Upper bounds of the second dimension: {0}", my2dArray.GetUpperBound(1))
    Here are the results. Told you it started at (1) but no one will listen.

    Lower bounds of the first dimension: 1
    Upper bounds of the first dimension: 27
    Lower bounds of the second dimension: 1
    Upper bounds of the second dimension: 3

    So I'm accessing, or trying to, Row 2 Col 2. Should be no problems even if this did start at index (0).

    EDIT: Here is a walk through of what is going on.....

    [code]
    Dim x As Boolean = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_CLOSE, cCOL_START))
    CK_AUTO_CLOSE_MAIN.Checked = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_CLOSE, cCOL_START))

    [/code

    Because of the error I placed a variable to read the output. The output is TRUE, so the read from the array is correct.

    Here is what the array GetOptionMember looks like.

    Code:
    Public Function GetSettingsMember(ByVal nRow As Integer, ByVal nCol As Integer) As String
        GetSettingsMember = m_objSettings_arr(nRow, nCol)
    End Function
    Here is the excel sheet first couple of read items. They all match up, but the error still persists.

    Code:
    Default Option	|DO Setting	|Comment
    Main Auto Close	|TRUE	|Closed the Tab if it has a "Close" button (After Updates)
    Main Auto Assign	|TRUE	|Re-assigns the Main to the user
    EDIT: For sanity sake I placed (x) as the input to the checkbox, but it still generated an error.

    Code:
    szErr = "Error occured for the following reason [Index was outside the bounds of the array.]"
    Last edited by FunkMonkey; Jun 11th, 2025 at 07:53 PM.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by OptionBase1 View Post
    If the OP would post the code that actually reads the spreadsheet and populates the array that would probably help.

    That being said, the OP hasn't stated if this error is still occurring or if it has been fixed. An Index Out of Bounds error should be a trivial problem to track down and fix. Do the values of the indices used when the error occurs match with the expectation of what they should be? If not, why not? Is it possible that events are causing your code to try to access the array before it has been populated?

    Lastly, and this was touched on in another thread, but ENUM_PALOOZA makes it difficult to follow what code is doing. I'm not saying it is bad or good or that you need to change it, but for an outsider to look at it and offer help, it adds a level of complication that will likely make some people not bother trying to help.
    Here is the code. Hope you see something, but this code has been working just fine.

    Code:
    Public Function LoadTab(ByVal strTab As String) As Object
    
        Dim bStatus As Boolean
        Dim rng As Excel.Range
        Dim dataArray(,) As Object
    
        'Dim Obj As Object
    
        Try
            EPIQ.Cursor = Cursors.WaitCursor
    
            SelectExcelSheet(strTab)
    
            If (GetIsDebug()) Then
                Debug.Print("GetExcel [LoadTab Enter]")
            End If
    
            bStatus = True
    
            rng = xlWS.UsedRange
    
            'Get the items in the tab
            dataArray = CType(rng.Value2, Object(,))
    
            LoadTab = dataArray
    
            'Get the items in the tab
            'LoadTab = xlWS.Range(xlApp.Selection, xlApp.Selection.End(xlDown)).CurrentRegion
    
            If (GetIsDebug()) Then
                Debug.Print("GetExcel [LoadTab Exit]")
            End If
    
        Catch ex As Exception
            Dim szErr As String
            szErr = "Error occured for the following reason [" & ex.Message & "]"
            If (GetIsDebug()) Then
                Debug.Print(szErr)
            End If
            bStatus = False
            LoadTab = Nothing
            dataArray = Nothing
        End Try
    
        EPIQ.Cursor = Cursors.Default
    
    End Function
    Yes the error is still there. Have started working in another area of the code to see what replies I get.

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    Ok. It’s unusual for VB to create a 1 based array, which was proven by debugging as suggested by dday.
    The next step is to use reflection to iterate through all of your enums, outputting the name and value, in a similar way to what you used for the array bounds…

  25. #25
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,909

    Re: [Index was outside the bounds of the array.] But it's not

    I went back to look at the original message. You said that the code keeps jumping to the catch.

    Exception handling is great, but it can get in the way of tracking down something like this. I can't remember which version of VS you are using, so this may be out of date, but I think you are in a fairly recent version: Under Debug | Windows, there is an option for Exception Settings. By default, the Common Language Runtime Exceptions is probably not checked. The checkbox is one of those trinary boxes, and you probably have it either unchecked or 'partial'. Check the box.

    What this does is that execution will break when the exception is thrown rather than just jumping down to the exception handler. This will allow you to identify exactly which line is throwing the exception. When you have that, you can look at the values in that line, and should be able to see why the array is out of bounds.
    My usual boring signature: Nothing

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by Shaggy Hiker View Post
    I went back to look at the original message. You said that the code keeps jumping to the catch.

    Exception handling is great, but it can get in the way of tracking down something like this. I can't remember which version of VS you are using, so this may be out of date, but I think you are in a fairly recent version: Under Debug | Windows, there is an option for Exception Settings. By default, the Common Language Runtime Exceptions is probably not checked. The checkbox is one of those trinary boxes, and you probably have it either unchecked or 'partial'. Check the box.

    What this does is that execution will break when the exception is thrown rather than just jumping down to the exception handler. This will allow you to identify exactly which line is throwing the exception. When you have that, you can look at the values in that line, and should be able to see why the array is out of bounds.
    Thanks Shaggy

  27. #27
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,132

    Re: [Index was outside the bounds of the array.] But it's not

    That's better than my suggestion. I was just going to get you to do Console.WriteLines after each read attempt until it failed.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    I'd still recommend debugging ALL of your Enum values. This exception is almost definitely because you have a variable, or more likely an Enum value, that is not what you're expecting it to be.

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by .paul. View Post
    I'd still recommend debugging ALL of your Enum values. This exception is almost definitely because you have a variable, or more likely an Enum value, that is not what you're expecting it to be.
    It's not the ENUM's. I placed the (x), in the first post, as the input to the checkbox.checked = x, and it still did the catch. The (x) was True, and this still generated the error.
    Last edited by FunkMonkey; Jun 13th, 2025 at 03:47 PM.

  30. #30
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by FunkMonkey View Post
    It's not the ENUM's. I placed the (x), in the first post, as the input to the checkbox.checked = x, and it still did the catch. The (x) was True, and this still generated the error.
    Are you sure you don’t have your indices messed up… i.e…

    YourArray(y, x) instead of yourArray(x,y)???

  31. #31
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    If x is type object, try…

    Checkbox.Checked=CBool(x.ToString)

    Where CheckBox is a valid control name

  32. #32

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by .paul. View Post
    Are you sure you don’t have your indices messed up… i.e…

    YourArray(y, x) instead of yourArray(x,y)???
    No their not messed up. The results come back with the value they should. I have checked that, but I would not receive an error in this instance either way as I am calling myArray(2,2). This falls in the boundaries of the array. I wish it was that easy.

    I was thinking once, if the GUI module was screwed up but then I realized VB compiles it on any changes and the main form works just fine. The form giving this issue is the Options form which is called from the Main Form.

    I know somewhere Something was, or had to be changed, as it was working just fine then it stopped working on all checkboxes. Every other control on the form works just as it should.

  33. #33
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    How do you show the Options form???

    Is it …

    Code:
    frmOptions.Show
    Or…

    Code:
    Dim frm As New frmOptions
    frm.Show
    ???

    It makes much more difference than you’d think. In the first case, you could just use…

    Code:
    frmOptions.CheckBox1.Checked = CBool(x)
    But if the form isn’t shown as the default instance (previous code) then….

    Code:
    frm.CheckBox1.Checked=CBool(x)

  34. #34

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by .paul. View Post
    How do you show the Options form???

    Is it …

    Code:
    frmOptions.Show
    Or…

    Code:
    Dim frm As New frmOptions
    frm.Show
    ???

    It makes much more difference than you’d think. In the first case, you could just use…

    Code:
    frmOptions.CheckBox1.Checked = CBool(x)
    But if the form isn’t shown as the default instance (previous code) then….

    Code:
    frm.CheckBox1.Checked=CBool(x)

    Code:
                Dim mySelection As New DLG_RUN_SELECTIION
                'User select operation type
                'mySelction.StartUpPosition = 1
                mySelection.ShowDialog() 'vbModeless
    
                If (mySelection.DialogResult = DialogResult.OK) Then
    
    
                    'If ((mySelection.GetRunOption() = GetRunOption()) And (mySelection.GetRunOption() > -1)) Then
                    '    'Disable the Run Type combobox
                    '    UpdateRunControls(ENUM_RUN.eRUN_SELECT_RUN_OPTION_COMPLETE, "Please select a different option ...", "BTN_RUN_Click")
                    '    InitializeProgressBar(0)
                    '    mySelection.Dispose()
                    '    Exit Sub
                    '    'Else
                    '    'UpdateRunControls(ENUM_RUN.eRUN_SELECT_RUN_OPTION_COMPLETE, cMSG_RUNNING, "BTN_RUN_Click")
    
                    'End If
                    SetRunOption(mySelection.GetRunOption())
                    'Close the selection window
                    mySelection.Dispose()
    The call with (x) is from a boolean type (x). It is confirmed to be "True". I would not be surprised if it was just 1 checkbox, but it's all of them.

    Code:
                Dim x As Boolean = CBool(GetOptionMember(ENUM_OPTIONS.eROW_OPTIONS_MAIN_AUTO_CLOSE, cCOL_START))
                CK_AUTO_CLOSE_MAIN.Checked = x
    EDIT: when opening the Options form, I see the checkbox is checked. Been tracking down the Catch error and did not see it. Anyway, here is the Dlg in question.

    Name:  Screenshot 2025-06-13 182002.jpg
Views: 224
Size:  24.7 KB
    Last edited by FunkMonkey; Jun 13th, 2025 at 06:21 PM.

  35. #35
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    Typically, you’d pass the data in the constructor…

    Code:
    Dim mySelection As New DLG_RUN_SELECTIION([all of the current options to set initial values])
    Then in the dialog..

    Code:
    Public Sub New(data as WhateverType)
        CheckBox1.Checked=data(0).someBoolean
        ‘ Etc
    End sub

  36. #36
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    You’d probably want to save data as a form (dialog) level variable, then make sure it contains all of your latest option data before setting your dialog DialogResult property to close the dialog, or to return as unchanged in the case of cancelling.
    When returning after closing the dialog, you’d then deal with your options…

  37. #37

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2025
    Posts
    323

    Re: [Index was outside the bounds of the array.] But it's not

    Quote Originally Posted by .paul. View Post
    Typically, you’d pass the data in the constructor…

    Code:
    Dim mySelection As New DLG_RUN_SELECTIION([all of the current options to set initial values])
    Then in the dialog..

    Code:
    Public Sub New(data as WhateverType)
        CheckBox1.Checked=data(0).someBoolean
        ‘ Etc
    End sub
    That is not possible, there are a good 50-60 controls on this Dlgbox. There are 7 tabs with 4 or more controls on each of them. I pass the Main Form OptionsArr through a SetOptionsArr for the DLG.

  38. #38
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    It's the .Net way... It's called Object Oriented Programming...

    https://learn.microsoft.com/en-us/do...ed-programming

  39. #39
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    I've exhausted my ideas on this, but...


    No further code will run in your main form, until you close the dialog

    Code:
    If (mySelection.ShowDialog() = DialogResult.OK) Then
    
        ' Any data that you want to grab from the dialog should be here
    
    End If

  40. #40
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: [Index was outside the bounds of the array.] But it's not

    I did warn you that my solution would look nothing like your solution. I'm not sure how you're passing and retrieving data to and from your dialog, but it's incorrect... Have a look here. I can't make it any simpler.

    http://www.scproject.biz/Using%20Dialogs.php#bm11

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width