Demystifying The Code

ASP.NET AJAX Client Library Question And Answer Series – Question 3 (How do you create static members and methods on an object in asp.net ajax?)

Question: How do you create static members and methods on an object in asp.net ajax?

Answer:

Members can be defined as attributes (fields) and methods in the context of oo programming.  So, in order to effectively answer the question, I need to illustrate how to create a public static field and method.  Take a look at the following Person (ASP.NET AJAX) class:

PersonStatic

Notice the species static field and the showSpecies static method.  In the code below, you can see where we are calling the static method and accessing the static field. 

CallStatic

You will also notice above that we are calling a static alert method on the JavaScript String Object.  Take a look at the following code, where I extended the String signature with an alert static method. 

StringExt 

So, not only can we create static members for our own objects, we can extend any object.

Have a question to add to the list?  Email me at rob.bagby@microsoft.com.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Demystifying The Code