Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

November 2009 - Posts

Visual Studio 2010 new Intellisense enhancements

In this post I am going to demonstrate through (screenshots mostly) some very handy, useful new features regarding Intellisense that ship with VS 2010. Through these features we can  be more productive.


Intellisense is very useful and has been an integral part of VS since its first release back in 2001. Microsoft's main aim is to to have "Intellisense everywhere"

 1) Create with VS 2010 an ASP.NET website.Save your website in your local file system.

 2) If you want to insert an update panel for example in the Default.aspx page, you can simply type left angle <up . When you do that you will see something similar to the picture below.....

3) Now if you press Tab twice you will see something similar to the picture below. We have the whole code for an update panel with a few keystrokes.

 

 Please note that this new enahanced Intellisense works equally well for Html Controls. If I type for example <img and hit the TAB twice, I will have something like <img src="#" alt="Alternate Text" /> completed for me.

4) We have great Intellisense support for Javascript as well.If I type script and hit TAB once, I get

<script type="text/javascript"> </script>

Then if inside my javacript code area type function and hit TAB once, I will get something like this.

function myfunction() {

              }

 Have a look at the picture below

 

Please note that we do have JS Intellisense inside the JS function.

5) Switch to the Default.aspx Designer View, drag and drop a repeater control in the .aspx page.

6) Go to the Default.aspx.cs file and in the Page_Load event type Repeater1.temp

Now of course we have Intellisense as we used to have but we also have more options.

See the picture below.

We have TemplateControl as an option but we also have AlternatingItem Template and Footer Template.

So Intellisense is more "clever" and shows us all the options with the "temp" argument in them... not just the properties-methods that start with "temp"


 

We have more intelligent Intellisense for Pascal Casing.

For more information about Pascal casing click here .

 if you type Repeater1.DB you will get Repeater1.DataBind

but if you type Repeater1.db you get nothing. That is because new Intellisense "honours" the Pascal casing syntax.

Have a look at the picture below

 

Nullable data types in Visual Basic 9.0

I know we are in Visual Studio 2010 and VB 10.0 version of the language-compiler.Most people I know (in my country anyway) work still with VS 2008 and .Net 3.5 SP1 version.

In this post I would like to talk about language improvements in visual basic 9.0 and more specifically about nullable data types.

The largest improvement in Visual basic 9.0 is LINQ. So all these enhancements that were made in the VB 9.0 language (the same goes for c# 3.0 enhancements) had LINQ in mind and how LINQ will be able to work.

Nullable data types enable us to create data types e.g integers and set them-initialise them to the value nothing, null.

In our database design we often have tables where fields are allowed to have NULL.

Prior to Visual Basic 9.0 one should turn the null value from the database into a zero value in the application.


Nullable data types help us to avoid this mismatch in LINQ to SQL.

The problem is that representing a null (nothing) value with zero is wrong, because null means something that is uknown and by setting it to zero, well you set it to something that is known.

In order to demonstrate this enhancement-new feature I will create a simple Asp.Net application using VB and Visual Studio 2008.

1) Launch Visual Studio 2008

2) Create a new project (ASP.NET web application)  and choose VB as the project’s language

3) Name it “nullabledata” or any other name you want

4) Add a button in the default.aspx page

5) Double click in the button

6) In the event handling routine that is created just type the following

Dim mynum As Integer? = 4
Dim myothernum As Integer? = Nothing
Dim myres As Integer = 0

myres = mynum + myothernum

Response.Write(IsNothing(myres).ToString())

by adding the ? operator after the data type, we indicate that the variable mynum can be set to nothing.

We declare another variable in this statement

Dim myothernum As Integer? = Nothing

This can only be valid in VB 9.0

Try and run your application by hitting F5.

Click on the button. You will receive an error – exception.

the problem is that the variable with the name myres is not nullable.

When you add 4 with an uknown value you do not get 4. You get an uknown value.

The solution to this problem is to create another variable that is nullable this time.

Dim myfinalres As Integer?

comment out these lines

myres = mynum + myothernum

Response.Write(IsNothing(myres).ToString())

and type these lines

myfinalres = mynum + myothernum

Response.Write(IsNothing(myfinalres).ToString())

Now if you run the application again and press the button you will receive the value of Nothing.
How to use extension methods with C# 3.0

In this post I would like to talk about a new feature in C# 3.0 that is called extension methods.

Extension methods are used extensively with LINQ, so it is of vital importance that we get a good understanding of extension methods. The reason why extension methods are so important is because this is how LINQ methods like Select,Where methods operate and work.

We can define them as static methods that extend existing classes without having to rely on inheritance or having to change the class’s source code or recompile the class. In the past to achieve the same thing we had to inherit from the selected class and extend it with new methods, if it was not sealed….

In order to better understand extension methods we will create a project in visual studio.

1) Launch Visual studio 2008

2) Create an asp.net web application in C# and call it “extensionmethods”

3) Add a new item to your application, a class file, and name it “myveryusefulextensionmethods.cs

4) Inside your class file you have somehing like this

namespace extension_methods
{
    public class myveryusefulextensionmethods

change the public class myveryusefulextensionmethods with 

static class myveryusefulextensionmethods

basically what we need to understand is that we must have a static class.Inside this static class we can add static methods.

5) We will create a static method inside our static class that just reverses a string. Please note that i use the keyword “this”. Whatever the input parameters are they must be prefixed with the “this” keyword. The code follows

 public static string strReverse(this string theString)
        {
            char[] strArray = theString.ToCharArray();
            Array.Reverse(strArray);
            string strReversed = new string(strArray);
            theString = strReversed;
            return theString;
        }

6) Now we can use this method from our default.aspx page to reverse a string. We will call this extension method from the page load event handler. The code follows

 protected void Page_Load(object sender, EventArgs e)
        {
            string s = “extensionmethods”;
       
            Response.Write(s.strReverse());
        }

When we type Response.Write(s.) we see something like the following picture in our code editor window.

 

Did you spot the new extension method “strReverse” among the other methods? This extension method is highlighted with a blue arrow icon facing down.

7) Run your application by hitting F5. You will get a new web page with “sdohtemnoisnetxe” on it. This is the reverse of “extensionmethods”

We can add more static methods to our static class.

Things to note when using extension methods

  • Use them when you really need them
  • Of course you cannot use extension methods to override existing methods
  • They are called extension methods for a a reason, so no you cannot apply this concept to fields and properties

 

More Posts