How do I fix index out of range error message in C#? [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
4 answers
Getting an index out of range error message on line 49 "if (myNumber == myPartyAges[i])" I can't figure out why. using Visual studio basic set up here is the code:
List<int> myPartyAges = new List<int>() { 24, 44, 54, 39, 17, 24, 21 };
int myNumber = myPartyAges[5];
bool contains = false;
for (int i = myPartyAges[5] - 1; i >= 1; i--)
{
if (myNumber == myPartyAges[i])
{
contains = true;
break;
}
}
if (contains == true)
{
Console.WriteLine("Number found.");
}
else
{
Console.WriteLine("Number not found.");
}
c#
marked as duplicate by David
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
4 answers
Getting an index out of range error message on line 49 "if (myNumber == myPartyAges[i])" I can't figure out why. using Visual studio basic set up here is the code:
List<int> myPartyAges = new List<int>() { 24, 44, 54, 39, 17, 24, 21 };
int myNumber = myPartyAges[5];
bool contains = false;
for (int i = myPartyAges[5] - 1; i >= 1; i--)
{
if (myNumber == myPartyAges[i])
{
contains = true;
break;
}
}
if (contains == true)
{
Console.WriteLine("Number found.");
}
else
{
Console.WriteLine("Number not found.");
}
c#
marked as duplicate by David
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
use a debugger it will help you understand. Tip watch the value of i on each increment
– Harry
Jan 4 at 13:20
1
istarts out as 23. There are only 7 values inmyPartyAges.
– Palle Due
Jan 4 at 13:27
I don't understand what you're talking about. Could you please explain that. I saw that in Vis studio but I didn't know what it meant.
– enterjcs
Jan 4 at 13:40
add a comment |
This question already has an answer here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
4 answers
Getting an index out of range error message on line 49 "if (myNumber == myPartyAges[i])" I can't figure out why. using Visual studio basic set up here is the code:
List<int> myPartyAges = new List<int>() { 24, 44, 54, 39, 17, 24, 21 };
int myNumber = myPartyAges[5];
bool contains = false;
for (int i = myPartyAges[5] - 1; i >= 1; i--)
{
if (myNumber == myPartyAges[i])
{
contains = true;
break;
}
}
if (contains == true)
{
Console.WriteLine("Number found.");
}
else
{
Console.WriteLine("Number not found.");
}
c#
This question already has an answer here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
4 answers
Getting an index out of range error message on line 49 "if (myNumber == myPartyAges[i])" I can't figure out why. using Visual studio basic set up here is the code:
List<int> myPartyAges = new List<int>() { 24, 44, 54, 39, 17, 24, 21 };
int myNumber = myPartyAges[5];
bool contains = false;
for (int i = myPartyAges[5] - 1; i >= 1; i--)
{
if (myNumber == myPartyAges[i])
{
contains = true;
break;
}
}
if (contains == true)
{
Console.WriteLine("Number found.");
}
else
{
Console.WriteLine("Number not found.");
}
This question already has an answer here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
4 answers
c#
c#
edited Jan 4 at 13:21
Peter B
13.8k52046
13.8k52046
asked Jan 4 at 13:19
enterjcsenterjcs
11
11
marked as duplicate by David
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by David
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 13:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
use a debugger it will help you understand. Tip watch the value of i on each increment
– Harry
Jan 4 at 13:20
1
istarts out as 23. There are only 7 values inmyPartyAges.
– Palle Due
Jan 4 at 13:27
I don't understand what you're talking about. Could you please explain that. I saw that in Vis studio but I didn't know what it meant.
– enterjcs
Jan 4 at 13:40
add a comment |
use a debugger it will help you understand. Tip watch the value of i on each increment
– Harry
Jan 4 at 13:20
1
istarts out as 23. There are only 7 values inmyPartyAges.
– Palle Due
Jan 4 at 13:27
I don't understand what you're talking about. Could you please explain that. I saw that in Vis studio but I didn't know what it meant.
– enterjcs
Jan 4 at 13:40
use a debugger it will help you understand. Tip watch the value of i on each increment
– Harry
Jan 4 at 13:20
use a debugger it will help you understand. Tip watch the value of i on each increment
– Harry
Jan 4 at 13:20
1
1
i starts out as 23. There are only 7 values in myPartyAges.– Palle Due
Jan 4 at 13:27
i starts out as 23. There are only 7 values in myPartyAges.– Palle Due
Jan 4 at 13:27
I don't understand what you're talking about. Could you please explain that. I saw that in Vis studio but I didn't know what it meant.
– enterjcs
Jan 4 at 13:40
I don't understand what you're talking about. Could you please explain that. I saw that in Vis studio but I didn't know what it meant.
– enterjcs
Jan 4 at 13:40
add a comment |
1 Answer
1
active
oldest
votes
Did you mean to do
for (int i = 4; i >= 0; i--)
instead of
for (int i = myPartyAges[5] - 1; i >= 1; i--)
?
I don't want to search the whole length. I want to start at a specific index.
– enterjcs
Jan 4 at 13:40
Ok you can do this, but not at index 23 like in the code you posted... I changed my code accordingly.
– Compufreak
Jan 4 at 15:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Did you mean to do
for (int i = 4; i >= 0; i--)
instead of
for (int i = myPartyAges[5] - 1; i >= 1; i--)
?
I don't want to search the whole length. I want to start at a specific index.
– enterjcs
Jan 4 at 13:40
Ok you can do this, but not at index 23 like in the code you posted... I changed my code accordingly.
– Compufreak
Jan 4 at 15:20
add a comment |
Did you mean to do
for (int i = 4; i >= 0; i--)
instead of
for (int i = myPartyAges[5] - 1; i >= 1; i--)
?
I don't want to search the whole length. I want to start at a specific index.
– enterjcs
Jan 4 at 13:40
Ok you can do this, but not at index 23 like in the code you posted... I changed my code accordingly.
– Compufreak
Jan 4 at 15:20
add a comment |
Did you mean to do
for (int i = 4; i >= 0; i--)
instead of
for (int i = myPartyAges[5] - 1; i >= 1; i--)
?
Did you mean to do
for (int i = 4; i >= 0; i--)
instead of
for (int i = myPartyAges[5] - 1; i >= 1; i--)
?
edited Jan 4 at 15:21
answered Jan 4 at 13:21
CompufreakCompufreak
2,12711131
2,12711131
I don't want to search the whole length. I want to start at a specific index.
– enterjcs
Jan 4 at 13:40
Ok you can do this, but not at index 23 like in the code you posted... I changed my code accordingly.
– Compufreak
Jan 4 at 15:20
add a comment |
I don't want to search the whole length. I want to start at a specific index.
– enterjcs
Jan 4 at 13:40
Ok you can do this, but not at index 23 like in the code you posted... I changed my code accordingly.
– Compufreak
Jan 4 at 15:20
I don't want to search the whole length. I want to start at a specific index.
– enterjcs
Jan 4 at 13:40
I don't want to search the whole length. I want to start at a specific index.
– enterjcs
Jan 4 at 13:40
Ok you can do this, but not at index 23 like in the code you posted... I changed my code accordingly.
– Compufreak
Jan 4 at 15:20
Ok you can do this, but not at index 23 like in the code you posted... I changed my code accordingly.
– Compufreak
Jan 4 at 15:20
add a comment |
use a debugger it will help you understand. Tip watch the value of i on each increment
– Harry
Jan 4 at 13:20
1
istarts out as 23. There are only 7 values inmyPartyAges.– Palle Due
Jan 4 at 13:27
I don't understand what you're talking about. Could you please explain that. I saw that in Vis studio but I didn't know what it meant.
– enterjcs
Jan 4 at 13:40