News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Self Identifiers

Started by thiruvasagamani, Jun 15, 2009, 01:11 PM

Previous topic - Next topic

thiruvasagamani

Self Identifiers

A self identifier is a name that represents the current instance. Self identifiers resemble the this keyword in C# or C++ or Me in Visual Basic. You can define a self identifier in two different ways, depending on whether you want the self identifier to be in scope for the whole class definition or just for an individual method.

To define a self identifier for the whole class, use the as keyword after the closing parentheses of the constructor parameter list, and specify the identifier name.

To define a self identifier for just one method, provide the self identifier in the member declaration, just before the method name and a period (.) as a separator.

The following code example illustrates the two ways to create a self identifier. In the first line, the as keyword is used to define the self identifier. In the fifth line, the identifier this is used to define a self identifier whose scope is restricted to the method PrintMessage.
Copy Code

type MyClass2(data_in) as self =
   let data = data_in
   do
       self.PrintMessage()
   member this.PrintMessage() =
       printf "Creating MyClass2 with Data %d" data

Unlike in other .NET languages, you can name the self identifier however you want; you are not restricted to names such as self, Me, or this.

The self identifier that is declared with the as keyword is not initialized until after the let bindings are executed. Therefore, it cannot be used in the let bindings. You can use the self identifier in the do bindings section.

Source : MSDN
Thiruvasakamani Karnan