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

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page