143
Views
2
Comments
Handle Index out of range errors better

I'm wondering if anyone has ideas on how to handle a possible index out of range exception

Scenario is this: 

I have a list of up to 5 possible records

I want to add these into a structure with 5 attributes, 1,2,3,4,5

so List[0] to should be added to the structure attribute 1, List[1] added to structure attribute 2

The issue is I can't just assign all because the List might only have 2 records, and on an assign of List[2] I'll hit an exception because of index being out of range.


I have an alternative solution using a switch and count etc... but I was wondering if there's a method to select an element from the List at a position and if its out of range, just treat as a null and continue?

2019-08-28 11-43-22
Bruno Marques

Hi, 

Please see my attached oml. Basically, you have to put an if condition before you assign anything to your structure.

Regards

Sample.oml
UserImage.jpg
Hussain Adam

Thanks Bruno, that solution does work and would make my code a bit better. I didn't think of using current row number.

It's similar to my solution but I was using a count instead of current row number, see example attached

I'd like to leave this question open a bit longer, if anyone has a solution for handling index select at position in a different way

Something like how this is done in c# https://stackoverflow.com/questions/14771149/how-to-handle-index-out-of-range-in-a-better-way

Sample.oml
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.