0

Struggling to assign objects to an array in C#.

The output of the code is unexpected because I get the nameOfProject.ClassName(?) x3 rather than name and life points of 3 monsters i added using my for loop. Debug suggests monsters aren't being assigned. Is there a logical flaw that people like me should be looking out for?

class Monsters
{
    public Monsters(string name, int points)
    {
        var monsterName = name;
        var monsterLifePoints = points;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Monsters[] monster = new Monsters[3];

        for (int i = 0; i < monster.Length; i++)
        {
            Console.WriteLine("Please enter a name");
            string name = Console.ReadLine();
            Console.WriteLine("Please enter life points");
            int points = Convert.ToInt32(Console.ReadLine());
            monster[i] = new Monsters(name,points);
            Console.Clear();
        }
        foreach (var element in monster)
        {
            Console.WriteLine(element);
        }
        Console.ReadKey();

    }
5
  • 3
    Your classname should not be plural. Commented Jul 6, 2017 at 12:52
  • Assignment works just fine. You never store the name, points though Commented Jul 6, 2017 at 12:52
  • 2
    You never told it how to print a Monsters, so that's what you get. Commented Jul 6, 2017 at 12:52
  • youtube.com/watch?v=u-HdLtqEOog Commented Jul 6, 2017 at 12:58
  • You have multiple problems here.. First you are not storing values of name and points in properties or private variables of class. Second you don't have any logic of how an object of monster class will be represented as string. Commented Jul 6, 2017 at 12:58

2 Answers 2

4

Assignment works just fine. The problem is that you never asign the monster's name and points to a field or property:

class Monsters
{
    public Monsters(string name, int points)
    {
        var monsterName = name;
        var monsterLifePoints = points;
    }
}

This code just assigns the input to local values and then discards them.

Your class should look like this:

class Monsters
{
    public string Name {get;set;}
    public int Points {get;set;}
    public Monsters(string name, int points)
    {
        Name = name;
        Points = points;
    }
}

This line still won't print the details :

Console.WriteLine(element);

You need to either create a string from the properties :

Console.WriteLine($"{element.Name} {element.Points}");

Or override the ToString() method of your class :

 public override string ToString()
{
    return $"{element.Name} {element.Points}";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help. Your answer has given me much to think about. Does the get allow me to retrieve data stored in the variables and set allow me to assign values to the variables. Is it considered good practice to do this always
0

How should WriteLine know that you want to print those two properties and not e.g. the type-name (which it does per default) or any other wierd string? You have to tell your program how to do this. This is done by overriding ToString

class Monster
{
    public override string ToString() {
        return this.name + " " + this.lifePoints;
    }
}

Furthermore your variables need to be fields:

class Monster
{
    private readonly string name;
    private readonly int lifePoints;

    Monster(string name, int points)
    {
        this.name = name;
        this.lifePoints = points;
    }
}

2 Comments

Those are fields, not instance variables. There is no such thing as "instance variable". It's better to use properties though.
@PanagiotisKanavos You´re right, updated. Why one should use properties isn´t clear to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.