c#


C# IndexOutOfRange Array Exception


I'm trying to create a 2D char array to hold a grid of chars which will be used as a sort of 'map' for a 2D console game.
I am getting a:
IndexOutOfRange exception
..and cannot see why. I've stepped through the code in debug mode and still cannot see the issue.
It steps through the code fine until it hits X = 25 and Y = 1, the upper right boundary of my grid.
I have _gameWidth and _gameHeight created as follows, outside of main but still inside the class:
static int _gameWidth = 25;
static int _gameHeight = 15;
Following is the code that fails, when trying to generate and populate the grid. It fails at this point:
else if (x == _gameWidth && y == 1)
_grid[x, y] = '╕';
static void GenerateGrid()
{
for (int y = 1; y <= _gameHeight; y++)
{
for (int x = 1; x <= _gameWidth; x++)
{
if (x == 1 && y == 1)
_grid[x, y] = '╒';
else if (x == _gameWidth && y == _gameHeight)
_grid[x, y] = '╛';
else if (x == _gameWidth && y == 1)
_grid[x, y] = '╕';
else if (x == 1 && y == _gameHeight)
_grid[x, y] = '╘';
else if ((x != 1 && y == _gameHeight) || (x != _gameWidth && y == 1))
_grid[x, y] = '═';
else if ((x == 1 && y > 1 && y < _gameHeight) || (x == _gameWidth && y > 1 && y < _gameHeight))
_grid[x, y] = '│';
else
_grid[x, y] = 'x';
}
Console.WriteLine("");
}
}
This exception means that you have accessed an invalid index. From the way you have written the loop I can tell that you think that indexes go from 1 to the length of the array. Arrays are zero-based, though. Use the standard loop form:
for (int i = 0; i < length; i++)
Your loop starts at one. You can use the Visual Studio for loop template. Just type "for<tab><tab>".
Your program might benefit from the Code Review Stack Exchange site.
Change
for (int i = 1; i <= gameHeight; i++)
to
for (int i = 0; i < gameHeight; i++)
and do the same for width.
EDIT:
This is because array indexes start at the number 0 and end with the length of the array minus 1.

Related Links

Unable to override crypto providers in Mono-2.10.8
One instance of control sharing property with another?
Capturing Desktop activities as recorded movie in C#
QueryString converted to URL-Encode using NameValueCollection
IQueryable C# Select
Is there a better way to do a server-client application?
The use of 'where' in C# [duplicate]
Order by and Group in NHibernate QueryOver
Powershell output formatting
C# / XNA - 2D collision engine glitch?
Creating a GraphicsPath from a semi-transparent bitmap
Programming to interfaces - Facebook C# SDK
FTP upload in .net — not getting correct file path in some browsers
how to insert newline in string in c# after some particular digit?
Olson Time Zone Id to Windows Standard Format using Noda Time
Using Microsoft Interop to read and write into excel cell

Categories

HOME
classification
max
twitter-bootstrap
marie
stanford-nlp
mobile
cluster-analysis
aix
phantomjs
boxplot
x509certificate
redhawksdr
pandoc
xss
schema
wavefront
ephesoft
decoding
opendaylight
openstack-horizon
dat.gui
project-structure
url-parameters
bug-reporting
multipart
functional-dependencies
datastax-enterprise
sha
ocl
semantic-ui-react
undo
multilanguage
akamai
jsonapi-resources
traitsui
n1ql
mootools
odp.net
wunderground
dom4j
rtsp
yelp
instances
gem-fury
file-conversion
tapply
file-descriptor
vpython
firefox-developer-edition
grails-2.0
risc
yank
geminabox
opos
openxava
google-cloud-shell
ear
beanshell
vertex
activesupport
htop
hellosign
blackberry
multi-select
acceleo
google-scholar
cedar-bdd
consolidation
hdr
tinybox2
airwatch
cherry-pick
nimbus
php-java-bridge
custom-build-step
factorization
imageicon
webservices-client
autoscaling
sicstus-prolog
maven-archetype
qf-test
powershell-v1.0
angular-file-upload
sendy
enquire.js
elixir-framework
iwork
data-layers
xtify
code-complete
ie-developer-tools
ia-32
coinbase-php
microsoft-speech-platform
yii-components
volatility
doxia
shapado
eeprom
pinchzoom
rjs
parallel-python
pex-and-moles
overlays
s60
mysql-logic
web-widget
retrospectiva
projectgen
moores-law

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App