Showing posts with label visual studio 2008. Show all posts
Showing posts with label visual studio 2008. Show all posts

Tuesday, 28 April 2009

Extension Methods in C# 3.0

A new function of C# 3.0 is Extension Methods.

"Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type."

For example, if you wanted a method to convert a temperature from celsius to fahrenheit, where previously you would use

int temp = 30;
int i = myClass.CelsiusToFahrenheit(temp);


using extension methods you can now use

int temp = 30;
int i = temp.CelsiusToFahrenheit();

To do this you would create a new static class to contain the static method CelsiusToFahrenheit. The key to extension methods is passing in the this keyword along with the variable. For example,


public static class myClass
{
public static int CelsiusToFahrenheit(this int temp)
{
return (9 / 5) * temp + 32;
}
}


This will now allow you to use the CelsiusToFahrenheit method on any integer variables.

You can also add methods to extend your own custom classes, and the methods can have different return types. For example, if you have your own class, TestClass, you can create a static method using extending TestClass, e.g.


public static DateTime doSomething(this TestClass t)
{
//do something
}


Which will then allow you to use:

TestClass t = new TestClass();
DateTime x = t.doSomething();

Sunday, 18 January 2009

webdev.webserver error

I got the above error in VS.NET 2008 when running an ASP.NET app. When trying to debug or show in browser the IDE gives the error in the standard "report to microsoft" dialog box followed by the following message "unable to connect to the asp.net development server".

To fix this error:
  • Right click on the project in solution explorer.

  • Hit F4 for properties (don't right click for properties).

  • Change "Use Dynamic Ports" to false and enter a new port number. Use one quite close to the current port number, I used 1080. Default was 1071.

Monday, 13 October 2008

LINQ in Visual Studio 2008

To use LINQ in a project add a 'LINQ to SQL Classes' template.

this opens a .dbml file that you can drag tables onto to make classes and stored procedures to make methods.
When you drag tables across there relationships are detected.

To use linq you need to create a Data Context to connect to your classes and methods.
This is usually called 'Whatever you named your LINQ to SQL Classes' + DataContext, eg.

PDPDataContext db = new PDPDataContext();

Get Data

To get all users you might do somehting like this:

var users: = from o in db.Users
select o;

This would select every record and field from the users table.

You can then bind this like you would a dataset or datatable.

datasource = users;

To insert a record using linq we can do the following:

Create an instance of the user class created by the LINQ to SQL Classes.

User u = new User();

Set the properties:

u.Name = "Jim";

Add User:

db.Users.InsertOnSubmit(u);

Save to Database:

db.SubmitChanges();


Update a record

Get a single record:

User u = (from o in db.User
where o.userid = 2456
select o).Single();

You set the properties as before and save to the database, there is no need to InsertOnSubmit.

db.SubmitChanges();

Examples
You can group and order and use aggregate functions using LINQ, for examples go here

Wednesday, 17 September 2008

Visual Studio 2008 Interesting features

Visual Studio 2008


JavaScript Features

JavaScript support in Visual Studio 2008 has been greatly increase and now has intellisense similar to C# coding. It also allows us to debug our JavaScript by the user of breakpoints and stepping through code. This will be a huge benefit to the team as we can leverage our existing debugging skill set at the web presentation layer of our applications.

LINQ (VS 2008)
LINQ in Visual Studio 2008 provides standard development syntax for accessing data, regardless of where the data resides. For example, the same syntax can access either SQL Server or XML data. LINQ is used rather than TSQL inside the application language, such as C# or VB.
Entity Data Services
ADO.NET Data services in the 3.5 version of the .Net framework now allows for high level business objects to be created, such as Customers or Parts. These entities can be used rather than the standard method of returning individual rows and tables. If you’re using E-R (entity relationship) modelling, your objects in SQL will now match your modelling. There are several new ADO.NET frameworks that can access these entities such as the Line-of-Business (LOB) framework and the Entity Query Language (eSQL).
Image Library

Visual Studio now contains standardised images from Windows, Office and other Microsoft software to keep your software consistent with Microsoft. This will give our application a familiar look other Microsoft products and look very professional in a commercial environment.


Plug-ins

Visual Studio 2008 extends the ability to create plug in tools for the IDE. Plug-ins are not something that we as a department will develop, however there are a greater number of plug-ins to increase our productivity available on the web. For instance the Class Builder wizard plug-in reduced the amount of code for new classes. There are a number of these that are available for free.

Unit testing

Visual Studio 2008 ships with a built in unit testing that allow use to build on our existing testing skills. Currently we use an added tool, NUnit, to perform automated testing. With 2008 we will have another tool at our disposal to increase the options we have when it comes to the testing of our applications.

Visual SourceSafe

We currently use the 2005 version of this software. Visual Studio 2008 requires an update to be compatible with VS2008. This currently available for download from the Microsoft web site and will not require a license upgrade for Visual SourceSafe
Link:
http://www.microsoft.com/downloads/details.aspx?FamilyID=8a1a68d8-db11-417c-91ad-02aab484776b&displaylang=en

Improved HTML and CSS features

The latest version of Visual Studio provides a split view when developing the HTML presentation layer page. We can now view the HTML alongside the design view, removing the time spent swapping between views to see how our changes have affected the look of the page.

There is also a CSS Style manger that increases the support for CSS in Visual Studio and also provides intellisense. There are also a few very useful tools to help the developer use CSS. This is a relatively new area for the team and these new supporting tools will be great help in an area where we are currently lacking.


Code editing enhancements

Visual Studio 2008 provides many enhancements to the code edit in C# files. It provide the developer with a hot key(CTRL) to make the intelisense transparent to avoid hiding the code we are currently working, this was a really frustrating problem in the older versions.

There is also improved refactoring tools and one of the interesting ones checks what “Using” statements are need in a class file removing any unnecessary ones. This will reduce the file size if we have links to code libraries we don’t require.


Migration Options
Visual Studio 2008 provides Multi-Targeting Support. This means that a developer can develop applications in versions 2.0, 3.0 and 3.5. This allows us to deploy applications to machines without the latest framework installs.

As with previous version of Visual Studio, 2008 provides us with a Project upgrade tool to import older projects.

As Visual Studio is only development tool and not installed on a server there are no issues in regards to older applications. It will simply allow us to take advantage of the latest tools in the IDE to improve our productivity and ultimately the quality and security of our final products.
Free Hit Counter