-5

I want to set up the Company class to be able to use the Address declared in the public static void Main(), how can I create a field for address in the company class?

class Program
{
    public static void Main()
    {
        var address = new Address("56 Main St", "Mesa", "AZ", "38574");
        var testCompany = new Company("testCompany");
        
    }
    
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public Address(string Street, string City, string State, string ZipCode)
    {

    }
}

public class Company
{
    public string CompanyName { get; set; }

}
3
  • Hi Dathon, you should be a bit more explicit about your problem. Is your application crashing? Do you have any error message? What have you tried to solve your problem?
    – AarónBC.
    May 8, 2018 at 0:42
  • You got an extra brace and aren't setting any properties/fields. Ctor's don't set the fields automatically. Your code isn't doing anything useful. Lookup the basics of field and property initialization. May 8, 2018 at 0:49
  • 1
    If you want those statements to work, you need to update the constructors of your Company and Customer classes to accept an address, e.g. public Customer(string FirstName, string LastName, Address address) May 8, 2018 at 0:49

2 Answers 2

2

You almost have it. You just need to require an Address in your constructors and set a corresponding property with that passed in address.

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public Address(string street, string city, string state, string zipCode)
    {
        Street = street;
        City = city;
        State = state;
        ZipCode = zipCode;
    }
}

public class Customer
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }

    public Customer(long id, string firstName, string lastName, Address address)
    {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
        Address = address;
    }


}

public class Company
{
    public string CompanyName { get; set; }
    public Address Address { get; set; }

    public Company(string companyName, Address address)
    {
        CompanyName = companyName;
        Address = address;
    }
}

Then your desired code will work:

var address = new Address("56 Main St", "Mesa", "AZ", "38574");
var customer = new Customer("John", "Doe", address);
var company = new Company("Google", address);
-1

Add an Address field in the Customer and Company classes, e.g. public Address MyAddress { get; set; }.

You can then use it like this:

var company = new Company();
Company.MyAddress = address;

Not the answer you're looking for? Browse other questions tagged or ask your own question.