-1

I'm trying to print out the int items inside the list but I get the following output, which is obviously not what I want to print:

YSolution.Dice
YSolution.Dice
YSolution.Dice
YSolution.Dice
YSolution.Dice

Source code:

class Dice
{
    //defining variable and properties
    private bool _hold = false;

    private Random rnd;
    public int Current { get; private set; }

    public bool IsHolding
    {
        get { return _hold; }
        set { _hold = value; }
    }

public Dice()
    {
        Current = 0;
        rnd = new Random(Guid.NewGuid().GetHashCode());
        FRoll();
    }

    public int FRoll()
    {
        Current = rnd.Next(1, 7);
        return Current;
    }



class DiceCup
    {
        public List<Dice> Dices { get; } = new List<Dice>();

        public DiceCup(int count)

        {
            for (int i = 0; i < count; i++)
            {
                Dices.Add(new Dice());
            }

            foreach (Dice aDice in Dices)
            {
                Console.WriteLine(aDice);
            }
        }
 class Program
{
    static void Main(string[] args)
    {

        DiceCup nbd = new DiceCup(count);



    }
}

The method FRoll(); seem to not get called in the dice class for some reason, when a new item is added to the list.I really just want to print out the items in the List Dices but I do not get the output/result I want. Anyone who can spot the error?

  • Console.WriteLine is using the `ToString` in Dice to print the Class name and location. You could override the `ToString` method in the Dice Class to return the Current property. Documentation for overriding `ToString` can be found here (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method). – kyleJ Apr 04 '20 at 16:59

3 Answers3

1

Currently you're just calling ToString() on your Dice object. Since you haven't overriden ToString() this is just using the default object.ToString() implementation which returns the type name of the object (YSolution.Dice in your case).

You have a Current property on your dice that returns the value of the dice, if you call this method then that will return the value of the dice, which you can then print: change Console.WriteLine(aDice); to Console.WriteLine(aDice.Current);.

Alternatively, as others have pointed out, you can override ToString() on your Dice class to return the current value of the dice:

class Dice
{
    //defining variable and properties
    private bool _hold = false;

    private Random rnd;
    public int Current { get; private set; }

    public bool IsHolding
    {
        get { return _hold; }
        set { _hold = value; }
    }

    public Dice()
    {
        Current = 0;
        rnd = new Random(Guid.NewGuid().GetHashCode());
        FRoll();
    }

    public int FRoll()
    {
        Current = rnd.Next(1, 7);
        return Current;
    }

    public override string ToString()
    {
        return Current.ToString();
    }
}
simon-pearson
  • 1,601
  • 8
  • 10
1

you want to implement the ToString-method:

public string ToString()
{
  return Current.ToString();
}
draz
  • 793
  • 6
  • 10
1

Besides overriding the ToString method, as mentioned in other answers, you could also collect the results from the dice and print those:

foreach (int result in Dices.Select(d => d.Current))
{
    Console.WriteLine(result);
}

The Select method is defined in the System.Linq namespace.

Vera
  • 644
  • 5
  • 10