0

I can't seem to successfully run the program. I'm only a couple weeks into learning c# and I can't seem to find a solution to this problem..

Error Image Link

What I have to do is implement a loop into this project and I was thinking about looping my DisplayInfo method to display all 4 houses as well as all the info about them at the same time.

public class Houses
{
    
    public string Address;
    public string Color;
    public int Price;
    public int RoomNumbers;

   
    public void DisplayInfo()
    {
            Console.WriteLine($"Address: {Address}");
            Console.WriteLine($"The color of the propery is: {Color}");
            Console.WriteLine($"The price of this property is valued at ${Price}");
            Console.WriteLine($"This property has {RoomNumbers} rooms.");
  
    }
}
class Program
{

    static void Main(string[] args)
    {
        
        Houses[] house = new Houses[4];
        {
            house[0].Address = "55 New Bridge Ln.";
            house[0].Color = "Red";
            house[0].Price = 200000;
            house[0].RoomNumbers = 2;


            house[1].Address = "101 Coders Dr.";
            house[1].Color = "Yellow";
            house[1].Price = 250000;
            house[1].RoomNumbers = 3;

            house[2].Address = "2041 Hungry Man Cir.";
            house[2].Color = "Green";
            house[2].Price = 350000;
            house[2].RoomNumbers = 5;

            house[3].Address = "3080 Falmoth Dr.";
            house[3].Color = "Purple";
            house[3].Price = 400000;
            house[3].RoomNumbers = 6;
        }
        for (int i = 0; i < house.Length; i++)
        {
            house[i].DisplayInfo();

        }
    }

} }

rioV8
  • 24,506
  • 3
  • 32
  • 49
TommyOnIt
  • 13
  • 4
  • 3
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details which can be done with a [mre]. Please [edit] your question to add these details into it or we may not be able to help. – gunr2171 Apr 19 '22 at 11:37
  • 1
    you are initializing your array in wrong way, it should be like this: var houses = new Houses[4]; houses[0] = new Houses(); houses[0].Address = "" etc Or better: use object initializer house[0] = new Houses{ Address = "", Color = ""...}; – Kamil Budziewski Apr 19 '22 at 11:41
  • why you dont create new house instances in your House array? – Barış Can Yılmaz Apr 19 '22 at 11:41
  • 2
    As an aside... Naming is important, and will help you continue to understand your code. Currently in your code a *single instance* of this object is called "Houses" (plural) and a *list of* this object is called "house" (singular). You're setting yourself up for ongoing confusion. – David Apr 19 '22 at 11:50
  • I got the impression that your class shall represent a single house, so I would rename it from "Houses" to "House". Than, you could created an array like so: House[] houses = new House[4]; Before making assignments to a single house, you must instanciate one with the new statement: House house1 = new House(); and houses[0] = house1; You can also write houses[0] = new House() if you want to start directly with the array. Than you can make the assignments. – Martin Apr 19 '22 at 11:57
  • Thank you all for your input! I really do appreciate it! – TommyOnIt Apr 19 '22 at 12:22

1 Answers1

3

you are missing object initialization for single house, you can do it like this:

    var house = new Houses[]
    {
        new Houses{
          Address = "55 New Bridge Ln.",
          Color = "Red",
          Price = 200000,
          RoomNumbers = 2
        },
        new Houses{
          Address = "33 New Bridge Ln.",
          Color = "Orange",
          Price = 300000,
          RoomNumbers = 1
        }

        ///...
    };
    for (int i = 0; i < house.Length; i++)
    {
        house[i].DisplayInfo();
    }

in addition you can move to foreach loop

    foreach(var singleHouse in house)
    {
        singleHouse.DisplayInfo();
    }

in general I would use a constructor in your Houses class

public class Houses
{
    
    public Houses(string address, string color, int price, int roomNumbers)
    {
        Address = address;
        Color = color;
        Price = price;
        RoomNumbers = roomNumbers;
    }

...

    var house = new Houses[]
    {
        new Houses("55 New Bridge Ln.", "Red", 200000, 2),
        new Houses("33 New Bridge Ln.", "Orange", 300000, 1)
        ///...
    };
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105