• No results found

Working with Source Code

In document Introduction to Visual Studio 2010 (Page 36-41)

You have seen the Synergy .NET source code editor a few times now, but so far we haven’t really  explored its capabilities. Let’s do that next. 

As you would expect, the source code editor is also a very powerful tool. It is both language and context  sensitive, and can help you write code correctly first time, layout that code in an appropriate way, and in  many cases can actually write some of the code for you. 

Imports

The concept of importing namespaces was first introduced in Synergy Language 9.1, when support for  object oriented (OO) development was introduced. In traditional Synergy, writing OO code is optional,  but in Synergy .NET everything is OO … whether you realize it or not. Yes, you can still use subroutines  and functions, but under the hood the environment is making those things methods on a class. In .NET …  everything is OO! 

In OO programming, types (classes, interfaces, enumerations, structures, etc.) are organized into  namespaces. If these types are defined within a project then they are usually all part of the same  namespace and become part of the assembly being developed. This means that (subject to accessibility  rules defined in the code) they can be used within the assembly by simply naming the type. 

However, if types are defined in a different assembly (project) then they are not automatically available  for use, and are generally going to be part of a different namespace. In this case, if we want to be able to  use the types in an external assembly then we add a reference to that assembly, as was discussed and  demonstrated earlier. 

Having added a reference to an assembly, we can access the public types in the assembly, but to do so  we would typically need to fully name each type that we wish to use, each time we reference it. For  example, imagine that we have added a reference to an assembly called MyUtils.dll. In the assembly is a  class called DateUtils, and that class is part of the MyUtils namespace. The class has three public static  methods called DaysBetween(), AddDays() and SubtractDays(). 

If we wanted to refer to one of those methods in code in our project, we would need to fully name  them, including their namespace. Our code might look something like this: 

paymentDue = MyUtils.DateUtils.AddDays(invoiceDate,30) 

While there is nothing wrong with this code, it could be simplified. If we have referenced another  assembly and we are going to use the types in that assembly frequently, we have the option of  importing the namespace of the types that we wish to use. Importing the namespace makes the  compiler aware of the types in the namespace, and means that we don’t have to fully name the types  each time we need to refer to them (unless there is a duplicate type in two or more namespaces that we  have imported). 

   

So, we could import the namespace by adding code like this at the very top of the source file: 

import MyUtils 

And then we could write the code that uses those types without mentioning the namespace: 

paymentDue =DateUtils.AddDays(invoiceDate,30) 

In the code editor for Form1.dbl, above the namespace definition and with the existing import  statements, add a new import for the System.Diagnostics namespace, like this: 

Scroll down into the button1_Click method and remove the namespace prefix for the Debug   class, like this: 

 

Importing a namespace is totally optional, but can make the code that uses the types in that namespace  much less verbose. 

IntelliSense

When you were typing the import statement above, you may have noticed that after you typed 

“import System.” something happened. You probably saw something like this: 

 

What happened was a Visual Studio feature called IntelliSense recognized what you were typing, and  stepped in to help. In this case, IntelliSense recognized that you were not currently inside a namespace,  and had started to type an import statement. IntelliSense knows that if you have typed an import  statement, then you also need to add a namespace, so it presented you a list of all of the namespaces  that were currently available in your project (based on the project itself, and the assemblies that you  have referenced). 

When this happens, you can continue to type, in which case the list of options will get gradually filtered  down to match the characters that you have typed so far: 

 

 

 

At any time during this process you can use the arrow keys or mouse to select a suggested value, and  then press enter,  tab, or space to insert that value into your code. If you don’t want the assistance then  you can hit the escape key to close the IntelliSense prompts … and of course you can just ignore what  it’s doing and keep typing. 

IntelliSense is context sensitive, so no matter where you are working in a piece of code you may find  that it jumps in there and offers shortcuts to what you’re typing. It can help you complete the names of  statements: 

  And types: 

 

And variables: 

  And parameters: 

  IntelliSense is very powerful, but until you get used to working with it, can also sometimes be a little  frustrating. The main reason for this results from the fact that the space key is one of the ways of asking  IntelliSense to complete what you typed. For example if you’re typing something, and IntelliSense is  offering you something that matches the characters that you have typed, you might instinctively press  the space bar to leave what you have typed and move on to the next word, but IntelliSense steps in and  replaces what you have typed with whatever match it was displaying when you pressed the space bar. 

The solution to this is to hit the escape key to dismiss IntelliSense before you press the space bar, but at  first this can be a little frustrating. However, it is well worth persevering and getting used to the way  that IntelliSense works, because once you master the art it can save you a lot of typing. 

Snippets

Another feature that can save you a lot of typing, particularly when you are writing new code, is a  feature called snippets. A snippet is similar in concept to an alias in Workbench. A snippet is a piece of  pre‐defined and named code that is inserted into the edit buffer, at the current cursor position, when  the name of the snippet is typed … followed by two tab characters. The first tab character selects the  snippet, and the second expands it. 

Visual Studio also presents snippets to you via IntelliSense, like this: 

 

You can tell that an item is a snippet because of its   icon. 

With a snippet selected, type TAB TAB to select and expand it. For example, the FOR snippet expands  like this: 

 

Some snippets just expand to a piece of fixed code, but most expand to a piece of code that includes  special tokens that are intended to be replaced with some other value by the programmer. You can see  these tokens in the example above, the i with the green background is the currently selected token, and  1, length and 1 with the yellow backgrounds are other tokens to be selected and replaced. 

With a snippet token selected, simply type the value that you want to insert, then press TAB to move on  to the next token or press ESCAPE to exit from token replacement completely. 

So, to fully use the FOR snippet, you could type: 

  for <tab> <tab> count <tab> 10 <tab> 1 <tab> ‐1 <escape> 

Which would result in the following code: 

 

While this may initially seem complicated, when you actually do it in the editor it is very natural, and can  save time. Another benefit of using snippets is that you’re pretty much guaranteed to produce the  correct code because the snippet does a lot of the work for you. Try it out: 

Place your cursor in the procedure division of the button1_Click method, after the  Debug.Print statement, on a new line 

Type for and press tab twice. 

Type var3 and press tab twice (once to dismiss IntelliSense, once to complete the snippet  token). 

Type tab to accept the default value 1. 

Type 10 to insert a custom value. 

Type <Escape> to complete the snippet expansion 

Notice how when you pressed the escape key, the cursor moved into the begin end block. That’s part of  the functionality of the snippet also. 

Remove the FOR loop from the code. 

By the way, if you have blocks of code that you use frequently, then you can also create your own code  snippets and add them to Visual Studio, but that’s a little beyond the scope of our objectives for this  tutorial. 

In document Introduction to Visual Studio 2010 (Page 36-41)

Related documents