c#


C# - I get the following error when I add a number while running my program 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll" [duplicate]


This question already has an answer here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
2 answers
So im trying to fill the courses on my program and when I click the Input button instead of proceeding to the next student I get this error "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll"
Here is the button
namespace WpfApplication1{
public partial class MainWindow : Window {
List<Students> student = new List<Students>();
int positionIndex = 0;
public MainWindow() {
InitializeComponent();
}
void btnExe_Click(object sender, RoutedEventArgs e) {
var course1 = Convert.ToDouble(txtCourse1.Text);
var course2 = Convert.ToDouble(txtCourse2.Text);
var course3 = Convert.ToDouble(txtCourse3.Text);
student[positionIndex].c1 = course1;
student[positionIndex].c2 = course2;
student[positionIndex].c3 = course3;
student[positionIndex].Name = positionIndex;
stdNames.Text = student[positionIndex].Name.ToString();
positionIndex++;
if (positionIndex == 6) {
btnExe.IsEnabled = false;
calculate();
};
}
Here is the class
namespace WpfApplication1 {
public class Students {
public int Name { get; set; }
public double c1 { get ; set; }
public double c2 { get; set; }
public double c3 { get; set; }
}
}
Thanks in advance, I really can't figoure out what is wrong.
Create an instance before accessing the item. student.Add(new Students());
void btnExe_Click(object sender, RoutedEventArgs e) {
var course1 = Convert.ToDouble(txtCourse1.Text);
var course2 = Convert.ToDouble(txtCourse2.Text);
var course3 = Convert.ToDouble(txtCourse3.Text);
student.Add(new Students());
student[positionIndex].c1 = course1;
student[positionIndex].c2 = course2;
student[positionIndex].c3 = course3;
student[positionIndex].Name = positionIndex;
stdNames.Text = student[positionIndex].Name.ToString();
positionIndex++;
if (positionIndex == 6) {
btnExe.IsEnabled = false;
calculate();
};
}

Related Links

Add same value into string multiple times
Checking for Nulls on DB Record Mapping
Check InnerException in System.TypeInitializationException thrown by an external code frame
Datagridview using excel file as datasource in c#
Accessing anonymous type variables
The provider is not compatible with this version of oracle client. ASP.net and oracle issue
Visual Studio 2013 Solution building not in build order
Combine WindowsAuthentication with rules stored in DB
I18N Rendering as EN-US and not Neutral Culture
Using generic or nor knowing type of a property in a method
Mapping DTO classes with Expression<Func> inside another Expression<Func>
Multi-Store development
Change text color of cells in Account column (C# datagridview)
Fluent API many to one relation
overlap between found contours
Guid constructor throwing formatException on apparently valid string

Categories

HOME
actionscript-3
spring-boot
http
skype-for-business
ckan
algolia
mobile
nagios
arcgis
hex
liquidsoap
collision-detection
install
samsung-gear-s2
soap-client
ethereum
mongoimport
wowza
minimum
console-application
exchange-transport-agents
url-parameters
devexpress-mvc
oracle-golden-gate
bootstrap-accordion
windowserror
deployment-descriptor
continuous-fourier
pydub
osgi-bundle
2checkout
selenium3
edsdk
wunderground
asp.net-webhooks
campaign-monitor
libconfig
jslider
lfe
ng-lightning
rbac
irr
peerjs
codepen
denodo
evaluation
ear
audio-fingerprinting
perldoc
enet
suitesparse
apache-commons-cli
msiexec
ownership
stripes
blackberry-webworks
gevent
distcc
xcode7.1
mate-desktop
particle-system
sprockets
bytearray
camellia
jmh
nugetgallery
matlab-engine
gtkwave
gnu-sort
android-facebook
personality-insights
om
tokudb
playn
clearquest
waterline
genero
maven-archetype
xmla
applinks
xcode5
uiblureffect
zoneminder
formbuilder
switchpreference
hotlinking
android-relativelayout
farpoint-spread
email-headers
mysql-error-1146
google-mirror-api
grunt-contrib-imagemin
kohana-orm
antixsslibrary
nsregularexpression
wpml
shapado
bindinglist
code-organization
clgeocoder
mscorlib
parallel-python
html5-animation
xslt-grouping
data-formats
cstring

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