Thursday 22 December 2011

Change the Default Project Values in .NET

You can change the default values of Option Explicit and Option Strict on a per project type basis. For example, when you create a new Visual Basic .NET or Visual Basic 2005 application, the value for Option Explicit is set to On. You can change this default value to Off.

To change the default values of Option Explicit and Option Strict, follow these steps:
  1. Locate the following project template files on your system:
    • EmptyProjectIPF.vbproj
    • EmptyWebProjectIPF.vbproj
    • WebApplication.vbproj
    • WebControl.vbproj
    • WebService.vbproj
    • WindowsApplication.vbproj
    • WindowsControl.vbproj
    • WindowsService.vbproj
  2. Open a project template in Notepad.
  3. Add (or edit if they are already present) the OptionStrict and OptionExplicit lines in the section of the template.

    For example, the following code demonstrates how to set OptionExplicit to Off and OptionStrict to On:
OutputType = "Exe"
StartupObject = ""
OptionExplicit = "Off"
OptionStrict = "On"
>
Repeat steps 2 and 3 for each project template that you want to change the default behavior for.

Wednesday 14 December 2011

Conversions in Dot Net (.Net) - Strict , Widening and Narrowing



Impact of Option Strict in Conversions (.NET)
Option Strict restricts implicit data type conversions to only widening conversions. Widening conversions explicitly do not permit any data type conversions in which data loss may occur and any conversion between numeric types and strings. For more information about widening conversions, see the Widening Conversions section.

When you use the Option Strict statement, the statement must appear before any other code. In Visual Basic .NET, you can typically convert any data type to any other data type implicitly. Data loss can occur when the value of one data type is converted to a data type with less precision or with a smaller capacity. However, you receive a run-time error message if data will be lost in such a conversion. Option Strict notifies you of these types of conversions at compile time so that you can avoid them.

Option Strict also generates an error message in the following scenarios:
  • For any undeclared variable. This is because Option Strict also implies Option Explicit. (Option Strict On requires all variable declarations to have an 'As' clause - Error ID: BC30209 )
To correct this error
1. Check to see if the As keyword is misspelled.
2. Supply an As clause for the declared variable, or turn Option Strict Off.
  • Late binding.
You will get the following error, if you attempt to execute the code below “Option Strict On disallows implicit conversions from 'Double' to 'Integer'”
Sub Convert_Double_To_int()
Dim DoubleVar As Double
Dim IntVar As Integer
DoubleVar = 123.456
IntVar = 2 + DoubleVar
End Sub



An important consideration with a type conversion is whether the result of the conversion is within the range of the destination data type. A widening conversion changes a value to a data type that can accommodate any possible value of the original data. A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values. The above conversion is an example of narrowing conversion
Widening Conversions
The following table lists the standard widening conversions.
Data Type
Widens to Data Types
Byte
Byte, Short, Integer, Long, Decimal, Single, Double
Short
Short, Integer, Long, Decimal, Single, Double
Integer
Integer, Long, Decimal, Single, Double
Long
Long, Decimal, Single, Double
Decimal
Decimal, Single, Double
Single
Single, Double
Double
Double
Any enumerated type
Its underlying integer type and any type to which it will widen
Char
Char, String
Any type
Object, any interface that it implements
Any derived type
Any base type from which it is derived
Nothing
Any data type or object type

The following conversions may lose precision:
  • Integer to Single
  • Long to Single or Double
  • Decimal to Single or Double
However, these conversions do not lose information or magnitude.

Widening conversions always succeed, and you can always perform widening conversions implicitly.
Explicit Conversion with Casting
An explicit conversion uses a type conversion keyword. Visual Basic .NET or Visual Basic 2005 provides several such keywords, which coerce an expression in parentheses to the data type that you want. These keywords behave as functions, but the compiler generates the code inline. Therefore, execution is a little faster with explicit conversion than with a function call.

The following table lists the available conversion keywords.
Type Conversion Keyword
Converts Expression
to Data Type
Permitted Data Types of Expression to Be Converted
CBool
Boolean
Any numeric type (including Byte and enumerated types), String, Object
CByte
Byte
Any numeric type, any enumerated type, Boolean, String, Object
CChar
Char
String, Object
CDate
Date
String, Object
CDbl
Double
Any numeric type (including Byte and enumerated types), Boolean, String, Object
CDec
Decimal
Any numeric type (including Byte and enumerated types), Boolean, String, Object
CInt
Integer
Any numeric type (including Byte and enumerated types), Boolean, String, Object
CLng
Long
Any numeric type (including Byte and enumerated types), Boolean, String, Object
CObj
Object
Any type
CShort
Short
Any numeric type (including Byte and enumerated types), Boolean, String, Object
CSng
Single
Any numeric type (including Byte and enumerated types), Boolean, String, Object
CStr
String
Any numeric type (including Byte), Boolean, Char, Char array, Date, Object
CType
Type specified following the comma (,)
When you convert to an elementary type (including an array of an elementary type), the same types as are permitted for the corresponding conversion keyword.

When you convert to a composite type, the interfaces it implements and the classes from which it inherits.

Monday 12 December 2011

Extract Interface from Custom Class (C#)

How to Extract Class Members to Interface in C#

You can extract one or more public members from a type into a new interface.
To extract members to a new interface
1. In Class Designer, right-click the type that contains the member or members you want to extract, point to Refactor, and click Extract Interface.
The Extract Interface dialog box displays default values for the name of the interface and the name of the code file in which it will be declared. Either accept the default values or change them.
2. In the Select public members to form interface pane, select or clear the check box next to the members you want to extract into the new interface, and click OK.
A new interface is created, and the file that houses it is added to the project.

Friday 9 December 2011

Visual Studio Add-ins vs. Shared Add-ins

Types of Add-Ins in Visual Studio

There are two different Add-in project types: Visual Studio and Shared.

A Visual Studio add-in can be loaded into both Visual Studio and the Visual Studio Macros IDE. Conversely, a Shared add-in can be loaded only into Microsoft Office applications such as Microsoft Word, Microsoft Publisher, Microsoft Visio, and Microsoft Excel. Also, each type offers a different set of options.

For example, the Visual Studio Add-in Wizard allows you to:

• Create a command bar user interface (UI) for your add-in,
• Define when the add-in loads, and
• Insert information into the Visual Studio Help About box.

The Shared Add-in Wizard only allows you to:

• Specify whether the add-in loads when the host application (that is, the Office application) loads, and
• Specify whether the add-in is available to all users of the computer or only the person who installed it.

In short, create Shared add-ins for use in Office applications, and create Visual Studio add-ins for use in Visual Studio.

Wednesday 7 December 2011

What is a static class?

Static classes are classes that do not contain instance members other than those inherited from Object, and do not have a callable constructor.
 
The members of a static class are accessed directly 
without an instance of the class.

Example
Console.WriteLine ("I Do not need any instances")

Tuesday 6 December 2011

Preventing multiple instances of .Net Application

Single instance application in VB.Net
If you were creating a Windows application, most often you would be doing some database initialization or some network validation before you show up the form to the user.
The patient user might wait few seconds to let the form show, others might launch the application again. To avoid user launch multiple instances of the application, select the Make single instance application check box to prevent users from running multiple instances of your application. The default setting for this check box is cleared, allowing multiple instances of the application to be run.
You can do this from ProjectàPropertiesàApplication tab

Friday 2 December 2011

Download Visual Studio 2008 and the .NET Framework 3.5

Microsoft announced that Visual Studio 2008 and the .NET Framework 3.5 were released to manufacturing (RTM). With more than 250 new features,Visual Studio 2008 includes significant enhancements in every edition, including Visual Studio Express and Visual Studio Team System. Developers of all levels – from hobbyists to enterprise development teams – now have a consistent, secure and reliable solution for developing applications for the latest platforms: the Web, Windows Vista, Windows Server 2008, the 2007 Office system, and beyond. Learn more about Visual Studio 2008. All of the above happening @ http://msdn2.microsoft.com/en-us/vstudio/default.aspx