BlancoBluesman
BlancoBluesman

Reputation: 13

Displaying a property of a class

I'm trying to display a property (myCar.Make) on a web form using the simpliest way, e.g. a literal control.

But I get errors.

How to do achieve this?

protected void Page_Load(object sender, EventArgs e)
{
    Car myCar = new Car();
    myCar.Make = "BMW";
    myCar.Model = "745li";
    myCar.Color = "Black";
    myCar.Year = 2005;

    Literal1.Text = "Car Make: {0}", myCar.Make;

}

Upvotes: 0

Views: 25

Answers (2)

Mister Epic
Mister Epic

Reputation: 16733

It looks like you are trying to format your string, but you need to call String.Format() to get it to work:

 Literal1.Text = String.Format("Car Make: {0}", myCar.Make);

Upvotes: 1

huMpty duMpty
huMpty duMpty

Reputation: 14470

Literal1.Text = "Car Make: " + myCar.Make;

or

Literal1.Text = string.Format("Car Make: {0}", myCar.Make);

Upvotes: 1

Related Questions