3 replies

Last post Aug 03, 2013 01:19 AM by Paul Linton

• cdf53

Member

24 Points

51 Posts

using try

I have a 2D int array and wish to traverse elements in a radius of 3 points from other specific points.

So if array[y,x] was the specific point:

for (int i = y-3, i<y+4,i++) { for (int j = x-3, j<xy+4,j++) { // LOGIC HERE }}

However since the 2d array is 25x25 dimensions, I would get an out of bound error if array[y,x] would be too close to the "border", or bounds.

What would be the best way of dealing with this? I know how to write the logic for it using if statements, but I was wondering what else I can do?

I thought of using try

```for (int i = y-3, i<y+4,i++) { for (int j = x-3, j<xy+4,j++) {

try {

//LOGIC HERE

}}}```

But I do not know if this would be proper technique, which is important to me.

Thanks!

• smirnov

All-Star

35159 Points

9075 Posts

Re: using try

You can check size of array using

myArray.GetLength(0) -> Gets first dimension size
myArray.GetLength(1) -> Gets second dimension size

`for (int i = y-3, i<y+4 && i<myArray.GetLength(0), i++) {  for (int j = x-3, j<xy+4 && j<myArray.GetLength(1),j++) { ... `
• Rion William...

All-Star

113020 Points

18361 Posts

MVP

Re: using try

Aug 01, 2013 08:57 AM|Rion Williams|LINK

I would highly recommend reviewing over the logic of your existing loop to help avoid these out of bounds exceptions in general such as by adding logic within your loops to ensure they don't exceed the bounds of your array using the GetLength() method:

```//Notice that you are checking the length to ensure the index isn't exceeded within your array
for (int i = y-3; i < y+4 && yourArray.GetLength(0); i++)
{
for (int j = x-3; j < xy+4 && yourArray.GetLength(1); j++)
{
}
}```

However you could use a try-catch block to handle these exceptions (and basically ignore them if they occur) as seen below :

```for (int i = y-3; i < y+4; i++)
{
for (int j = x-3; j < xy+4;j++)
{
try
{
//Attempt to perform your logic here
}
catch
{
//An error occurred - likely an Out-of-Bounds Exception you want to ignore
continue;
}
}
}```

• Paul Linton

Star

9555 Points

2784 Posts

Re: using try

Aug 03, 2013 01:19 AM|Paul Linton|LINK

Instead of directly access each cell with

array[i, j]

put the array access code in a method.  It depends on what is in the array but something like

```bool TryArray(int[] theArray, int x, int y, out int value)
{
value = 0;
if (x<0 || x > theArray.GetUpperBound(0)) return false;
if (y<0 || y > theArray.GetUpperBound(1)) return false;

value = array[x,y];
return true;
}```

In your main code you would do

```int value; \\This would be outside the loops
.
.
.
if(TryArray(array,i,j, out value)) {
// Do whatever you want with value
} else {
// It was outside the bounds so don't touch value
}```

The code could be tightened up and made more general but the basic idea is to provide a 'safe' way to access the array modeled on the exist family of TryXXX methods.

You should not use Exceptions for this sort of thing.  Exceptions should only be used for exceptional behaviour.  Of the 625 points in your array more than a third of them would cause at least one exception to be thrown - hardly exceptional.

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.