J. Koh
J. Koh

Reputation: 75

Object array in c#

I encountered several problems when creating storing objects to an Array and printing in c# after debugging. What is my problem here? The problem started occurring at adding the objects to the Array and printing the object title.

static void Main(string[] args)
    {
        ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
        Console.WriteLine(cg1.title);

        ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
        ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);
        ComputerGame[] gameAlbum = new ComputerGame[5];
        for (int i = 0; i < 5;i++)
        {
            gameAlbum[0] = new ComputerGame();
            gameAlbum[1] = new ComputerGame();
            gameAlbum[2] = new ComputerGame();
        }
        foreach(ComputerGame o in gameAlbum)
        {
            Console.WriteLine(o.title);
        }
    }

public class ComputerGame
{
    public string title;
    public double price;
    public ComputerGame(string title, double price)
    {
        this.title = title;
        this.price = price;

    }
}

Upvotes: 1

Views: 161

Answers (8)

Prasad Raja
Prasad Raja

Reputation: 703

Try with this code..

Looping the data & get sum of the price

         ComputerGame[] gameAlbum = new ComputerGame[5];
        gameAlbum[0] = new ComputerGame("Age of Empires", 49.99);
        gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
        gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);
        gameAlbum[3] = new ComputerGame("Portal", 19.50);
        gameAlbum[4] = new ComputerGame("Portal 2", 29.50);

        //looping the data
        foreach (ComputerGame item in gameAlbum)
        {
            if (item != null)
                Response.Write("Name : " + item.title + " Price : " + item.price);
        }
        //get the sum of the price
        double total = gameAlbum.Sum(p => p.price);
        Response.Write(total);

Upvotes: 0

Dr. Stitch
Dr. Stitch

Reputation: 918

Do this instead:

static void Main(string[] args)
{
    ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
    Console.WriteLine(cg1.title);

    ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
    ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);
    ComputerGame[] gameAlbum = new ComputerGame[5];

    gameAlbum[0] = cg1;
    gameAlbum[1] = cg2;
    gameAlbum[2] = cg3;

    foreach(ComputerGame o in gameAlbum)
    {
        if (o != null)
           Console.WriteLine(o.title);
    }

    double total = gameAlbum.Where(g => g != null).Sum(g => g.price);
}

Simpler way using a list instead of an array:

List<ComputerGame> games = new List<ComputerGame>();
games.Add(new ComputerGame("Age of Empires", 49.99));
games.Add(new ComputerGame("Heroes and Generals", 30.00));
games.Add(new ComputerGame("Team Fortress 2", 19.50));
games.Add(new ComputerGame("Portal", 19.50));
games.Add(new ComputerGame("Portal 2", 29.50));

foreach(ComputerGame game in games)
{
    if (game != null)
        Console.WriteLine($"Title: {game.title}, Price: {game.price}");
}

double total = games.Sum(p => p.price);

Upvotes: 2

Nikolai Arsenov
Nikolai Arsenov

Reputation: 464

Remove for loop. Create instances with parameters like you did. Sign it to array like gameObjs[0] = cg1, etc.

Upvotes: 1

Deepak S
Deepak S

Reputation: 45

The problem with your code is that the default constructor for ComputerGame is overridden. There is no constructor such as:

public ComputerGame()
{...
}

Hence, you can do as follows:

gameAlbum[0] = new ComputerGame("Age of Empires",49.99);
gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);

You might also get errors as you are making use of only 3 of the 5 array elements. Hence, use List<> instead of arrays for dynamically creating the objects when needed.

Upvotes: 0

Nifal Nizar
Nifal Nizar

Reputation: 963

I don't see any assignments to the array "gameAlbum" in your for loop.
Try the below one and see.

static void Main(string[] args)
{
    ComputerGame[] gameAlbum = new ComputerGame[3];

    gameAlbum[0] = new ComputerGame("Age of Empires",49.99);
    gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
    gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);

    foreach(ComputerGame o in gameAlbum)
    {
       Console.WriteLine(o.title);
    }
}

public class ComputerGame
{
    public string title;
    public double price;
    public ComputerGame(string title, double price)
    {
        this.title = title;
        this.price = price;

    }
}

Upvotes: 0

gotnull
gotnull

Reputation: 27214

Give this a go:

ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);

ComputerGame[] gameAlbum = new ComputerGame[5];
gameAlbum[0] = cg1;
gameAlbum[1] = cg2;
gameAlbum[2] = cg3;

foreach(ComputerGame o in gameAlbum)
{
    if (o != null)
        Console.WriteLine(o.title);
}

Upvotes: 0

Akshey Bhat
Akshey Bhat

Reputation: 8545

ComputerGame[] gameAlbum = new ComputerGame[5];
        for (int i = 0; i < 5;i++)
        {
            gameAlbum[0] = new ComputerGame();
            gameAlbum[1] = new ComputerGame();
            gameAlbum[2] = new ComputerGame();
        }

In the for loop, you are initiating the first three elements of array again and again. You are also calling the default constructor of ComputerGame class. So title element of ComputerGame won't get initialized. So you won't see anything printed on console.

Upvotes: 0

Mattias &#197;slund
Mattias &#197;slund

Reputation: 3907

Gamealbum 0, 1 and 2 are created with a parameterless constructor but you require two parameters.

Try addimg another constructor to your class or include the parameters in the new-statements.

On a sidenote, there is no reason to loop 5 times since the loop just does tje same thing all the time.

Upvotes: 0

Related Questions