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;








0
















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.











share|improve this question















marked as duplicate by Alexei Levenkov c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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

















0
















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.











share|improve this question















marked as duplicate by Alexei Levenkov c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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













0












0








0









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.











share|improve this question

















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#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















3














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++;







share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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++;







    share|improve this answer



























      3














      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++;







      share|improve this answer

























        3












        3








        3







        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++;







        share|improve this answer













        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++;








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 23:49









        Darrin CullopDarrin Cullop

        80469




        80469















            Popular posts from this blog

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived