Declaring Fields and Properties

You can define a property for a component in two ways: the lazy way and the virtuous way. The lazy way to create a property is to create a public field. If you declare any field with the Public access modifier, the field can be accessed from outside the component. For example, the component in Listing 17.6 contains a public field named Message.

Listing 17.6. FieldHelloWorld.cs

images

The Message field is declared near the top of the FieldHelloWorld class definition. The Message field is returned by the SayMessage() method.

The page in Listing 17.7 uses the FieldHelloWorld component to display a message.

Listing 17.7. ...

Get ASP.NET 4 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.