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

How to generate Entity Framework 6.x POCO classes with mappings from an EDMX file?
lambda innerjoin convert error
Matrix Multiplication with threads
Send XML file on request
Load a function after the layout is being added and shown completely
Updating the key object in AddOrUpdate method
ObjectDisposedException while using socket
How can I add a text box dynamically under an other by pressing a button in c#? (winforms)
VS2013 Dynamically create chart c#
.UpdateLayout() throwns an exception from RunWorkerCompleted method
Unix to Dos Conversion in C#
How to fill a textbox by using another textbox textchanged event?
NHibernate How do i put data in many to many mapping
Is there a way to use the Windows Form table in a Razor view cshtml?
Round robin pub/sub with StackExchange.Redis
Check if 'Restrict background data' is active on Windows Phone

Categories

HOME
classification
kendo-ui
postgresql-9.3
ldap
casting
phantomjs
pandoc
singleton
datetimepicker
thermal-printer
browserstack
pyodbc
mongoimport
elastalert
guidewire
android-notifications
lambda-calculus
taxonomy
template10
versioning
vivado-hls
scotty
webgl-earth
minimagick
monad-transformers
connectiq
oracle10g
traveling-salesman
csproj
semantic-ui-react
folder
lines
loopback
imdb
angularjs-resource
kofax
kepserverex
form-fields
queryover
mixed-models
vpython
firefox-developer-edition
smooth-streaming
gradlew
taleo
spring-ide
infovis
visualizer
gige-sdk
ajv
fckeditor
multi-targeting
huawei
sqlproj
mongodb-3.3
alfred
eclipse-mars
github-for-mac
dpkt
eula
teamwork
gcc-warning
sicstus-prolog
wingdb
fiware-health
cordova-3
random-access
os.walk
go-gin
dirname
worklight-appcenter
agent-based-modeling
oxygenxml
reactjs-native
pidgin
flask-mongoengine
db2-connect
ie-developer-tools
raddatepicker
kendo-window
cab
infomaker
brew
configurationsection
junit-rule
flatten
systrace
django-admin-tools
jqueryi-ui-buttonset
visual-leak-detector
pex-and-moles
qvariant
zipstream
cpack
chars
xslt-grouping
focus-stealing
weak-typing
ironpython-studio
paperless

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