Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# How can I add objects to a list of variable size from user input

sidepie101

New Coder
I am a newbie on C# and I am trying to make a program to handle orders from customers. I created my object and added a method to read the user input but I am still struggling with creating a list where user can add products by putting in their properties ( name, code and price).

Here is what I got so far :

public class Produit
{
public string? NomProduits { get; set; }
public string? CodeProduits { get; set; }
public string? PrixProduits { get; set; }
}

public class AttributsProduits
{

public static void AjouterProduits ()
{
Produit ReadProduits()
{
return new Produit()
{
NomProduits = Console.ReadLine(),
CodeProduits = Console.ReadLine(),
PrixProduits = Console.ReadLine(),
};
}
}
}


I created the object and its attributes, and added a method to read said attributes from the user. This is what I did for the list but Im getting an error CS1729 :

List<Produit> produits = new List<Produit>();

produits.Add(new Produit(NomProduits));
produits.Add(new Produit(CodeProduits));
produits.Add(new Produit(PrixProduits));

Can anyone help me with this please?

Adding objects to an existing list
 
First of all, your code is very hard to read with everything shifted left. Please post code in proper code tags. Also, post errors in their context so that it is clear on which line they occur (i.e. paste the compiler output). Does the text of the error not make clear what the issue is ? You don't have a constructor for class Produit that takes one string argument.
 
Last edited by a moderator:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom