How can i create an object from class from user? [duplicate]ArgumentOutOfRangeException on initialized ListHow to create a new object instance from a TypeHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Creating a byte array from a streamHow do you create a dropdownlist from an enum in ASP.NET MVC?How to get the type of T from a member of a generic class or method?How do I update the GUI from another thread?Creating a comma separated list from IList<string> or IEnumerable<string>How can I get the application's path in a .NET console application?How can I generate random alphanumeric strings?How to Sort a List<T> by a property in the object
A reference to a well-known characterization of scattered compact spaces
Neighboring nodes in the network
Is there a hemisphere-neutral way of specifying a season?
Forgetting the musical notes while performing in concert
Alternative to sending password over mail?
I Accidentally Deleted a Stock Terminal Theme
Why is Collection not simply treated as Collection<?>
AES: Why is it a good practice to use only the first 16bytes of a hash for encryption?
Is the Joker left-handed?
What about the virus in 12 Monkeys?
Assassin's bullet with mercury
How do conventional missiles fly?
Today is the Center
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
I would say: "You are another teacher", but she is a woman and I am a man
What does it mean to describe someone as a butt steak?
In a Spin are Both Wings Stalled?
How do I write bicross product symbols in latex?
Twin primes whose sum is a cube
How much of data wrangling is a data scientist's job?
Would Slavery Reparations be considered Bills of Attainder and hence Illegal?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Why doesn't H₄O²⁺ exist?
Is it possible to create light that imparts a greater proportion of its energy as momentum rather than heat?
How can i create an object from class from user? [duplicate]
ArgumentOutOfRangeException on initialized ListHow to create a new object instance from a TypeHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Creating a byte array from a streamHow do you create a dropdownlist from an enum in ASP.NET MVC?How to get the type of T from a member of a generic class or method?How do I update the GUI from another thread?Creating a comma separated list from IList<string> or IEnumerable<string>How can I get the application's path in a .NET console application?How can I generate random alphanumeric strings?How to Sort a List<T> by a property in the object
.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:
ArgumentOutOfRangeException on initialized List
1 answer
I've created a class "Person" and I want to do a while loop to let the user add objects until they want to stop, but it doesn't work.
I think the problem is I don't know how to create new objects from a list, but I'm unsure.
Here's my code:
static void Afficher(List <Personne> maliste)
foreach (var per in maliste)
per.ToString();
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
maliste[i] = new Personne();
maliste[i].nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].age = int.Parse(s);
i++;
Error happens on maliste[i] = new Personne();
line:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
c#
marked as duplicate by Alexei Levenkov
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();
);
);
);
Mar 9 at 2:44
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:
ArgumentOutOfRangeException on initialized List
1 answer
I've created a class "Person" and I want to do a while loop to let the user add objects until they want to stop, but it doesn't work.
I think the problem is I don't know how to create new objects from a list, but I'm unsure.
Here's my code:
static void Afficher(List <Personne> maliste)
foreach (var per in maliste)
per.ToString();
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
maliste[i] = new Personne();
maliste[i].nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].age = int.Parse(s);
i++;
Error happens on maliste[i] = new Personne();
line:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
c#
marked as duplicate by Alexei Levenkov
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();
);
);
);
Mar 9 at 2:44
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.
Somewhere based on something you will want to setstop
to true to break out of the loop. You will also want to be much, much clearer aboutit doesn't work
– Make StackOverflow Good Again
Mar 8 at 23:47
@MakeStackOverflowGoodAgain no, they havebreak
in several mis-formattedif
checks - so all good (while(true)
would be better, but...)
– Alexei Levenkov
Mar 9 at 2:45
add a comment |
This question already has an answer here:
ArgumentOutOfRangeException on initialized List
1 answer
I've created a class "Person" and I want to do a while loop to let the user add objects until they want to stop, but it doesn't work.
I think the problem is I don't know how to create new objects from a list, but I'm unsure.
Here's my code:
static void Afficher(List <Personne> maliste)
foreach (var per in maliste)
per.ToString();
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
maliste[i] = new Personne();
maliste[i].nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].age = int.Parse(s);
i++;
Error happens on maliste[i] = new Personne();
line:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
c#
This question already has an answer here:
ArgumentOutOfRangeException on initialized List
1 answer
I've created a class "Person" and I want to do a while loop to let the user add objects until they want to stop, but it doesn't work.
I think the problem is I don't know how to create new objects from a list, but I'm unsure.
Here's my code:
static void Afficher(List <Personne> maliste)
foreach (var per in maliste)
per.ToString();
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
maliste[i] = new Personne();
maliste[i].nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].age = int.Parse(s);
i++;
Error happens on maliste[i] = new Personne();
line:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
This question already has an answer here:
ArgumentOutOfRangeException on initialized List
1 answer
c#
c#
edited Mar 9 at 2:46
Alexei Levenkov
85.6k893140
85.6k893140
asked Mar 8 at 23:40
Reda TahaReda Taha
147
147
marked as duplicate by Alexei Levenkov
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();
);
);
);
Mar 9 at 2:44
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 Alexei Levenkov
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();
);
);
);
Mar 9 at 2:44
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.
Somewhere based on something you will want to setstop
to true to break out of the loop. You will also want to be much, much clearer aboutit doesn't work
– Make StackOverflow Good Again
Mar 8 at 23:47
@MakeStackOverflowGoodAgain no, they havebreak
in several mis-formattedif
checks - so all good (while(true)
would be better, but...)
– Alexei Levenkov
Mar 9 at 2:45
add a comment |
Somewhere based on something you will want to setstop
to true to break out of the loop. You will also want to be much, much clearer aboutit doesn't work
– Make StackOverflow Good Again
Mar 8 at 23:47
@MakeStackOverflowGoodAgain no, they havebreak
in several mis-formattedif
checks - so all good (while(true)
would be better, but...)
– Alexei Levenkov
Mar 9 at 2:45
Somewhere based on something you will want to set
stop
to true to break out of the loop. You will also want to be much, much clearer about it doesn't work
– Make StackOverflow Good Again
Mar 8 at 23:47
Somewhere based on something you will want to set
stop
to true to break out of the loop. You will also want to be much, much clearer about it doesn't work
– Make StackOverflow Good Again
Mar 8 at 23:47
@MakeStackOverflowGoodAgain no, they have
break
in several mis-formatted if
checks - so all good (while(true)
would be better, but...)– Alexei Levenkov
Mar 9 at 2:45
@MakeStackOverflowGoodAgain no, they have
break
in several mis-formatted if
checks - so all good (while(true)
would be better, but...)– Alexei Levenkov
Mar 9 at 2:45
add a comment |
1 Answer
1
active
oldest
votes
You can't use the array indexing (like maliste[i]
) when adding a new item. All you need to do is call maliste.Add
to insert a new item in the list. Your function should look like this:
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
var pers = new Personne();
maliste.Add( pers );
pers.nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.age = int.Parse(s);
i++;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't use the array indexing (like maliste[i]
) when adding a new item. All you need to do is call maliste.Add
to insert a new item in the list. Your function should look like this:
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
var pers = new Personne();
maliste.Add( pers );
pers.nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.age = int.Parse(s);
i++;
add a comment |
You can't use the array indexing (like maliste[i]
) when adding a new item. All you need to do is call maliste.Add
to insert a new item in the list. Your function should look like this:
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
var pers = new Personne();
maliste.Add( pers );
pers.nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.age = int.Parse(s);
i++;
add a comment |
You can't use the array indexing (like maliste[i]
) when adding a new item. All you need to do is call maliste.Add
to insert a new item in the list. Your function should look like this:
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
var pers = new Personne();
maliste.Add( pers );
pers.nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.age = int.Parse(s);
i++;
You can't use the array indexing (like maliste[i]
) when adding a new item. All you need to do is call maliste.Add
to insert a new item in the list. Your function should look like this:
static void Ajouter(List<Personne> maliste)
string s;
bool stop = false;
int i = 0;
while(!stop)
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
var pers = new Personne();
maliste.Add( pers );
pers.nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
pers.age = int.Parse(s);
i++;
answered Mar 8 at 23:49
Darrin CullopDarrin Cullop
80469
80469
add a comment |
add a comment |
Somewhere based on something you will want to set
stop
to true to break out of the loop. You will also want to be much, much clearer aboutit doesn't work
– Make StackOverflow Good Again
Mar 8 at 23:47
@MakeStackOverflowGoodAgain no, they have
break
in several mis-formattedif
checks - so all good (while(true)
would be better, but...)– Alexei Levenkov
Mar 9 at 2:45