Home > database >  How do I print out Array of objects with a set of values in each element C#
How do I print out Array of objects with a set of values in each element C#

Time:03-04

I am having trouble printing out my Array of objects. When I print out the objects, the values set inside each object does not get printed out. I only manage to print out "Name: Personal number: District: Sales:" but no values for them. How can I work through this? I have also tried override ToString but I get the same results there.

        public static void Main(String[] args)
        {

            Seller[] sellerArray = new Seller[amount];

            for (int i = 0; i < amount; i  )
            {
                sellerArray[i] = new Seller();
                sellerArray[i].setInformation();
            }

            for (int i = 0; i < sellerArray.Length; i  )
            {
                sellerArray[i].printSellers();
            }
        }

    class seller
    { 

    public String Name { get; set; }

    public String PersonalNumber { get; set; }

    public String District { get; set; }

    public String Sales { get; set; }

    public void setInformation()
    {

        Seller seller = new Seller();
        Console.WriteLine("Enter Name");
        seller.Name = Console.ReadLine();

        Console.WriteLine("Personal number");
        seller.PersonalNumber = Console.ReadLine();

        Console.WriteLine("District");
        seller.District = Console.ReadLine();

        Console.WriteLine("Sales");
        seller.Sales = Console.ReadLine();
        Convert.ToInt32(seller.sales);
        
        Console.WriteLine();
    }

    public void printSellers()
    {
        Console.WriteLine("Name: "   this.Name);
        Console.WriteLine("Personal number: "   this.PersonalNumber);
        Console.WriteLine("District: "   this.District);
        Console.WriteLine("Sales: "   this.Sales);
        Console.WriteLine();
    }
    }

CodePudding user response:

This is because in your setInformation() method, you're creating a new Seller and setting it's values. When you get each property value you want to store it to this. For example:

public void setInformation()
{
    Console.WriteLine("Enter Name");
    this.Name = Console.ReadLine();
    ...
}

You could also move the setInformation() method outside the class and have it return the newly created Seller. For example:

public static Seller SetInformation()
{
    Seller seller = new Seller();
    Console.WriteLine("Enter Name");
    seller.Name = Console.ReadLine();

    Console.WriteLine("Personal number");
    seller.PersonalNumber = Console.ReadLine();

    Console.WriteLine("District");
    seller.District = Console.ReadLine();

    Console.WriteLine("Sales");
    seller.Sales = Console.ReadLine();
    
    Console.WriteLine();
    
    return seller;
}

Edit

It looks like you have a couple other issues in the code you've provided.

First off, you have inconsistent capitalization all over. the Seller class is not capitalized (it should be) but in some places you use the capitalized version.

Next, the line Convert.ToInt32(seller.sales); isn't needed and will fail because the property name should be capitalized.

The variable amount is referenced but not set anywhere.

To summarize: Change the class name to Seller as it should be. Remove that unnecessary line, and define amount somewhere. With my these changes and my answer your code should run correctly.

  • Related