This document is the fifth in a set of tutorials on writing calculations in Service Desk. Each topic can be read independently but for an introduction to calculations please see the following document: Calculation Writing Tutorial - 1. What is a calculation? What is Boo?.
The next set of tutorials will introduce working with and manipulating specific types of data - Strings, Numbers (Int16, Int32 and Decimals), DateTimes, etc. This document is a primer to these to introduce the concepts used.
Some experience in object and process design is recommended, as well as the basic concept of calculations as covered in the first tutorial (linked above).
What is the .NET Framework and a .NET Class?
In a nutshell the .NET Framework is a large set of Classes (AKA objects, things) that programming languages can access. It was created by Microsoft as a way to standardise programming in a Windows environment. Service Desk itself is written with it and calculations can use it too.
Each class does a specific job, for instance there is a class called String which contains way of manipulating Strings, there is a class called IO for looking at files and folders, a class called XML for reading, parsing and writing XML files, etc. You can think of a .NET Class as a Service Desk object. Within a class you have Methods and Properties. Methods are functions and "do" something. Properties are like attributes in a Service Desk object and store specific information.
The .NET classes are arranged in a hierarchy, called namespaces. A lot of the basic classes for storing data such as String, DateTime, etc. are in the System namespace and can be called System.String, System.DateTime, etc. The class for working with colours is called Color and is under the System.Drawing namespace so its full name is System.Drawing.Color.
How does this fit in with Boo?
Boo uses the .NET Framework. Every time you reference an attribute you are referencing an instance of the .NET Class for that type of data - String, Int32, Decimal, DateTime, etc. Every time you create a variable that variable is actually an instance of a .NET Class and is decided automatically depending on what you're trying to store in the variable. For example the line Value = 'Some Text' creates a variable called Value and it will automatically be a String. The line Value = 12 will automatically create the variable as an Int32.
Boo itself controls the structure and syntax of the language - how "if" statements and "for" loops are arranged, how variables are set and referenced, etc. However the access and manipulation of data itself is all the work of .NET.
Some basic examples of using .NET Methods
There are two types of .NET methods used in Boo - static and instance. A static method is something you can call at any time, pass some parameters and get a value returned. An instance method is something you call from an instance of a class.
import System static def GetAttributeValue(Incident): ContainsCheck = Incident.Title.Contains('weather') if ContainsCheck == true: Value = 'This incident is about the weather'
This uses the instance method .Contains() on the Title attribute. Title is a String attribute and so it can access all the methods and properties of the String .NET class. This particular method takes a parameter and then returns a Boolean value of true if the String instance it was called against contains the String used in the parameter . This value is being stored in the variable ContainsCheck which is then being used in the "if" statement. You can also skip setting the returning value to a variable and use the method directly in the "if" statement:
import System static def GetAttributeValue(Incident): if Incident.Title.Contains('weather') == true: Value = 'This incident is about the weather'
This can be shortened further because "if" statements themselves create a true or false value to say if the statement is met or not. You can therefore simply use the return value of the method without any kind of comparison as follows:
import System static def GetAttributeValue(Incident): if Incident.Title.Contains('weather'): Value = 'This incident is about the weather'
In this case it also reads better - "If the incident Title contains 'weather' ......".
Here is an example of a static method in the String class:
import System static def GetAttributeValue(Incident): if String.IsNullOrEmpty(Incident.Description): Value = 'This incident has no description'
Static methods are accessed by using the class name as its prefix. In this case the IsNullOrEmpty() method takes a String parameter than returns a Boolean value of true if the value of the String provided in the parameter, in this case Incident.Description, is NULL or has an empty value. As with the last example because this method happens to return a Boolean we are using it directly within an "if" statement.
Earlier in this tutorial we learned that the String class lives in the System namespace, yet in the last example we were able to refer to String.IsNullOrEmpty() directly and not use its full name System.String.IsNullOrEmpty(). This is because at the very top of the calculation is the line import System. This means we're going to be using the classes in that namespace a lot so would rather not have to type in the namespace name every time.
This isn't such a chore for classes directly under System however gets more tiresome if you wanted to keep referencing classes under the System.Drawing. You can add more of these shortcuts by adding more lines at the top of the calculation:
import System import System.Drawing import System.Xml import System.Data.SqlClient static def GetAttributeValue(Incident): .....
As well as methods you will find Properties under each class. The difference in the syntax is that a method will always have brackets after it and can take parameters but a property is just referenced as it is named. In the String class there are only a couple of Properties, the most useful being .Length which returns an Int32 value with the number of characters in the String:
import System static def GetAttributeValue(Incident): if Incident.Title.Length > 50: Value = 'This incident has a title longer than 50 characters'
Exploring the .NET Framework
Microsoft provide full documentation on every class online on their MSDN website. Here is a direct link to the top of the namespaces tree and you can browse the available namespaces and the classes within each one: http://msdn.microsoft.com/en-us/library/gg145045. You can also always rely on Google to find the right page for you, for example if you search for System.String the first result is the Microsoft documentation that details the full list of methods and properties for that class.
As well as the Microsoft documentation there are tutorials and articles all over the internet to be found giving working examples of using any class you can think of. Chances are that any example code you find will be written in programming languages other than Boo (most likely Visual Basic and C#) but as this tutorial has hopefully explained this does not matter at all.