[This is an excerpt from the book, Microsoft AJAX Library Essentials: Client-side ASP.NET AJAX 1.0 Explained, by Cristian Darie, Bogdan Brinzarea. Published by Packt Publishing Ltd., 2007
This line of code effectively creates an object named
myHelloWorld, which represents an instance of the
ShowHelloWorld() function. When the object is instantiated, the function code is executed, so creating the object has the same effect as calling
ShowHelloWorld() as in the previous examples.
We'll demonstrate these concepts by transforming the
ShowHelloWorld() function that you saw earlier into a "real" class. We will:
ShowHelloWorld() as a class, we are changing its name to one that reflects this purpose. ]
Once your new class is created, you use it just as you'd use a C# class. For example, this is how you'd create a new class instance, and call its
A possible implementation of the
HelloWorld class is the following:
This code can be tested online. The
HelloWorld class is formed of the constructor code that initializes the hour field (
this.hour), and of the
this.DisplayGreeting(). Fans of the ternary operator can rewrite the constructor using this shorter form, which also makes use of the object detection feature that was discussed in Chapter 2:
(condition ? valueA : valueB). If the condition is true, the expression returns
valueA, otherwise it returns
valueB. In the shown example, object detection is used to test if a value was supplied for the
hour parameter. If it was not, the current hour is used instead. ]
HelloWorld class shown earlier would be described as shown in Figure 3-2.
HelloWorld class has an integer field named
The diagram also mentions the
HelloWorld() function receives a parameter named
hour, which is supposed to be an integer value.
Appendix A contains more details about the conventions used in class diagrams throughout this book.