user2915962
user2915962

Reputation: 2711

Acessing properties of an object with initializers

I have this code:

public class TopTen
{
    public int Id { get; set; }
    public string ShortDesc { get; set; }
    public string LongDesc { get; set; }
    public Image Photo { get; set; }
}

public class Image
{
    public string ImgUrl { get; set; }
    public string AlterText { get; set; }
}

I can do like this to assign values:

new topten()
{
/*
     here I can access and give values to my properties. BUT i cant
     access and give values to the properties in my `Image` class.
     This is a problem because I would like to upload the new object as a json-file.
     So how do I access the properties in `Image`?
*/
}

Upvotes: 1

Views: 48

Answers (1)

GrandMasterFlush
GrandMasterFlush

Reputation: 6409

var x = new TopTen
{
    Id = 1,
    Photo = new Image
    {
       ImgUrl = "pic.jpg",
       AlterText = "This is a picture"
    }
};

Upvotes: 4

Related Questions