This documentation is archived and is not being maintained.

Writing Code in LightSwitch

Visual Studio 2010

When you write code for your application, you will use the Code Editor. The code that you write in a LightSwitch application will mostly be in built-in methods, that is, methods of entities, screens, and queries. For example, every screen has a <ScreenName>_CanRun () method, where <ScreenName> is the name of the screen. You would typically write code in this method to check whether a user has permission to see the screen. For more information about how to write code in methods, see How to: Handle Data Events, How to: Handle Screen Events, or How to: Handle Query Events

For more advanced scenarios, you might write code that uses the LightSwitch object model. For example, you might write code that uses the data model to handle concurrency issues that occur when saving data. For more information, see Performing Data-Related Tasks by Using Code

You can use either the Visual Basic or C# programming language. Both are equally capable. It is a matter of personal choice. You cannot mix Visual Basic and C# code in a single project, and you must make the choice when you create the project.

More than just a text editor, the Code Editor uses a technology known as IntelliSense to help you write code by providing relevant information as you type. There are several features of IntelliSense that can make your coding tasks easier. These features include List Members, Parameter Info, Quick Info, Complete Word, and Syntax Tips.

List Members

When you type the name of a type or namespace in the Code Editor, a list of all the valid methods, properties, and events appears in a drop-down list. An example of code written in a method that displays the list members is shown in the following illustration.

List Members

List Members

You can scroll through the list or type the first few letters of the member to move to that member in the list. Then press ENTER to add that member to your code.

Parameter Info

When a method takes parameters, IntelliSense displays information about the parameters, such as the type of parameter, the name, and the number of parameters required. If a function is overloaded, you will see UP and DOWN arrows that let you to scroll through all the function overloads, as shown in the following illustration.

Parameter Info

Parameter Info with Overloads Image

As you type the parameter, the list displays the next parameter in bold font.

Quick Info

You can display the complete declaration of an identifier in your code by holding your mouse pointer over the identifier. The following illustration shows the Quick Info box that appears.

Quick Info

Quick Info Image

Complete Word

IntelliSense can complete words for you as you type. When you have typed enough letters in a word to disambiguate it from other words, you can complete the word by pressing ALT+RIGHT ARROW or CTRL+SPACEBAR. For example, you can type Me.Button1.Te to filter the words in the list, and then press ALT+RIGHT ARROW to complete the word.

The following illustration shows an example of the completion list that appears when you type code in the Code Editor.

Complete Word


Additional IntelliSense Features

  • Keyword IntelliSense lets you to see a list of keywords that are available, depending on the context. At the file-level, IntelliSense is available for keywords such as Imports and Option. You will also see a list of declaration-level keywords that filter based on the syntax, such as the For and While keywords.

  • Expression Lists appear when you type various expressions. For example, when you assign a value to a variable, an Expression List will appear after you type the equal sign.

  • Syntax Tips provide information about the syntax of a statement. For example, when you type the keyword Select, IntelliSense displays Select Case <testExpression>, which indicates the syntax to use.

  • Local variables appear in a list at the start of a line in a method, when you type arguments for a method call, and in any expression context for which they are in scope.

  • Transparent Lists. Sometimes you might find that code in the IDE that you have to reference is covered by an IntelliSense list. You can press the CTRL key to temporarily make the list transparent so that you can see the code underneath it. When you release the CTRL key, the list returns.

  • List Filtering. Instead of displaying the whole list, IntelliSense displays only the words that begin with the letters you have typed. For example, if you want to create a variable for a PrintDialog and you type Dim printDocument As New Print, the list will display only those members that begin with the text "Print". This makes it easier to find the item you are looking for. If you do want to see the complete list, you can press CTRL+J. When you start to type again, the list will again become filtered.