Windows Phone Developers

Sunday, August 24, 2008

Creating Simple Windows Forms Application using C#

1. Start Visual Studio.

2. Create a Windows Application called HelloWorld.

3. From the Toolbox, drag a Label control onto the form.

4. Double Click the label to add an event handler.

5. Insert the following code:

private void label1_Click(object sender, EventArgs e)

{

MessageBox.Show("Hello World");

}

How to create Windows Forms Application using C#



Create a Windows Application
Drag a Label
Drag a Label
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Avoiding Temporary Strings in C# / Reduce Unnecessary Garbage Collection in C#

Appending strings to existing will produce garbage collection overhead for the program. Instead use Append method of StringBuilder class or Concat, Join methods of String class

The following code differentiates between both :

private void stringbuilder_example()

{

// Appending String - Garbage Collection - Immutable

string CompleteAddress = "";

CompleteAddress += "105, Annanagar ";

CompleteAddress += "Chennai ";

CompleteAddress += "Tamil Nadu ";

MessageBox.Show(CompleteAddress);

// Appending String - using String Builder

System.Text.StringBuilder CompleteAddress1 = new

System.Text.StringBuilder();

CompleteAddress1.Append( "105, Annanagar ");

CompleteAddress1.Append( "Chennai ");

CompleteAddress1.Append( "Tamil Nadu ");

// Error Code

//MessageBox.Show(CompleteAddress1);

// corrected Code

MessageBox.Show(CompleteAddress1.ToString() );

}

C# StringBuilder Class, C# StringBuilder Class Append Method, Appending Strings without Garbage Overhead, Avoiding Temporary Strings in C#, Reduce Unnecessary Garbage Collection in C#

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Nullable Variables in C#

A type is said to be nullable if it can be assigned a value or can be assigned nullNothingnullptra null reference (Nothing in Visual Basic), which means the type has no value whatsoever. Consequently, a nullable type can express a value, or that no value exists. For example, a reference type such as String is nullable, whereas a value type such as Int32 is not. A value type cannot be nullable because it has enough capacity to express only the values appropriate for that type; it does not have the additional capacity required to express a value of null.

The Nullable<(Of <(T>)>) structure supports using only a value type as a nullable type because reference types are nullable by design.

The Nullable class provides complementary support for the Nullable<(Of <(T>)>) structure. The Nullable class supports obtaining the underlying type of a nullable type, and comparison and equality operations on pairs of nullable types whose underlying value type does not support generic comparison and equality operations.

A variable can be declared nullable as shown below

Nullable <bool> FlagComplete = null;

Scenario

Use nullable types to represent things that exist or do not exist depending on the circumstance. For example, an optional attribute of an HTML tag might exist in one tag but not another, or a nullable column of a database table might exist in one row of the table but not another.

You can represent the attribute or column as a field in a class and you can define the field as a value type. The field can contain all the valid values for the attribute or column, but cannot accommodate an additional value that means the attribute or column does not exist. In this case, define the field to be a nullable type instead of a value type.

Fundamental Properties

The two fundamental members of the Nullable<(Of <(T>)>) structure are the HasValue and Value properties. If the HasValue property for a Nullable<(Of <(T>)>) object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.

string Sample;

Nullable <bool> FlagComplete = null;

if (FlagComplete.HasValue)

{

MessageBox.Show(Sample);

}

else

{Sample = "No Value";

MessageBox.Show(Sample);

FlagComplete = false;

}

Boxing and Unboxing

When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the Nullable<(Of <(T>)>) object, not the Nullable<(Of <(T>)>) object itself. That is, if the HasValue property is true, the contents of the Value property is boxed. When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable<(Of <(T>)>) structure initialized to the underlying value.

If the HasValue property of a nullable type is false, the result of a boxing operation is nullNothingnullptr a null reference (Nothing in Visual Basic). Consequently, if a boxed nullable type is passed to a method that expects an object argument, that method must be prepared to handle the case where the argument is nullNothingnullptr a null reference (Nothing in Visual Basic). When nullNothingnullptra null reference (Nothing in Visual Basic) is unboxed into a nullable type, the common language runtime creates a new Nullable<(Of <(T>)>) structure and initializes its HasValue property to false.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

The best overloaded Add method 'name for the collection initializer has some invalid arguments in ‘ToString’

The type of one argument in a method does not match the type that was passed when the class was instantiated.

The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string)' has some invalid arguments

Solution : One possible solution is to check if the ‘ToString’ has braces

System.Text.StringBuilder CompleteAddress1 = new

System.Text.StringBuilder();

CompleteAddress1.Append( "105, Annanagar ");

CompleteAddress1.Append( "Chennai ");

CompleteAddress1.Append( "Tamil Nadu ");

‘ Error Line

MessageBox.Show(CompleteAddress1.ToString );

Solution

MessageBox.Show(CompleteAddress1.ToString() );

Argument '1': cannot convert from 'method group' to 'string'




The best overloaded Add method 'name for the collection initializer has some invalid arguments in ‘ToString’ Error
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Argument '1': cannot convert from 'System.Text.StringBuilder' to 'string'

Use ‘ToString’ to convert the StringBuilder to String

// Appending String - using String Builder

System.Text.StringBuilder CompleteAddress1 = new

System.Text.StringBuilder();

CompleteAddress1.Append( "105, Annanagar ");

CompleteAddress1.Append( "Chennai ");

CompleteAddress1.Append( "Tamil Nadu ");

// Error Code

//MessageBox.Show(CompleteAddress1);

// corrected Code

MessageBox.Show(CompleteAddress1.ToString() );



Argument '1': cannot convert from 'System.Text.StringBuilder' to 'string' Error
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Use of unassigned local variable 'name'

The C# compiler does not allow the use of uninitialized variables. If the compiler detects the use of a variable that might not have been initialized, it generates CS0165.

string Sample;

Nullable <bool> FlagComplete = null;

if (FlagComplete.HasValue)

{

MessageBox.Show(Sample);

}

Use new to create an instance of an object or assign a value

string Sample = "";

Nullable <bool> FlagComplete = null;

if (FlagComplete.HasValue)

{

MessageBox.Show(Sample);

}

else




If you want to have null for the variable Sample, declare it as Nullable ()
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the

An array was declared incorrectly. Be aware that the syntax for a fixed size buffer is different from that of an array. - Compiler Error CS0650

Wrong Declaration

char CopiedString[];

char[] CopiedString;

Correct Declaration

char[] CopiedString = new char[3];

Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

A new expression requires (), [], or {} after type (C# Excel Application)

The new operator, used to dynamically allocate memory for an object, was not specified correctly. (Compiler Error CS1526)

Replace

oXL = new Microsoft.Office.Interop.Excel.application;

with

oXL = new Microsoft.Office.Interop.Excel.application();

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Using Directive in C#, Create an alias in C#

Before you can use the classes in a given namespace in a C# program, you must add a using directive for that namespace to your C# source file. In some cases, you must also add a reference to the DLL that contains the namespace

The using directive has two uses:
· To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;


· To create an alias for a namespace or a type.

using Excel = Microsoft.Office.Interop.Excel

The using keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly. See using Statement for more information.
The scope of a using directive is limited to the file in which it appears.
Create a using alias to make it easier to qualify an identifier to a namespace or type.

Big Statements like the following

oXL = new Microsoft.Office.Interop.Excel.application();

can be replaced with

using Excel = Microsoft.Office.Interop.Excel
oXL = new Excel.application();

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify. Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Thursday, August 14, 2008

Adding and Subtracting Days/Months/Years to a given date using Vb.NET

DateAdd Function in VB.NET / AddDays Function in VB.NET

Sub DateAdd_Example()

Dim CurrentDate As Date

CurrentDate = DateTime.Now

MsgBox("Tomorrow is " & CurrentDate.AddDays(1) & " using AddDays")

MsgBox("Tomorrow is " & DateAdd(DateInterval.Day, 1, CurrentDate) & " using DateAdd")

End Sub

Subtracting Days/Months/Years using VB.NET

Days/Months can be subtracted from the given date by passing a negative value to the DateAdd or AddDays function

Sub DateSub_Example()

Dim CurrentDate As Date

CurrentDate = DateTime.Now

MsgBox("Yesterday was " & CurrentDate.AddDays(-1) & " using AddDays")

MsgBox("Yesterday was " & DateAdd(DateInterval.Day, -1, CurrentDate) & " using DateAdd")

End Sub

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Get No of Days in a month using VB.NET

Get No of Days in a month using VB.NET

Count no of days in Current Month using Vb.NET, VB.NET Get No of Days in a month

Sub Get_No_Of_Days()

Try

MsgBox("This month has " & NoOfDays(DateTime.Now.Year, DateTime.Now.Month) & " days")

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Function NoOfDays(ByVal MyYear As Integer, ByVal MyMonth As Integer) As Integer

Return DateTime.DaysInMonth(MyYear, MyMonth)

End Function

DayCount using VB.NET, .NET Function to Count No of Days in a month

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Identify LeapYear using VB.NET

Check LeapYear using VB.NET

Sub CheckLeapYear()

Dim MyDate As DateTime

MyDate = New DateTime(2008, 8, 15)

If DateTime.IsLeapYear(MyDate.Year) = True Then

MsgBox("Specified year is a leap year")

End If

End Sub

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

DateSerial Function in VB.NET / Check if the Date is in DayLightSaving Range using VB.NET

How to Check if the Date is in DayLightSaving Range using VB.NET

VB.NET’s DAteTime function can be used to give the same output VBA’s DateSerial function

Sub GetDate()

Dim MyDate As DateTime

MyDate = New DateTime(2008, 8, 15)

MsgBox(DateSerial(2008, 8, 15))

If MyDate.IsDaylightSavingTime() = True Then

MsgBox("Specified day is within daylightsaving time")

End If

End Sub


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Enabling Smart Tags in Word / Excel

Enabling Smart Tags in Word / Excel

To run a smart tag, end users must have the Label data with smart tags option enabled in Word or Excel.

To enable smart tags in Word 2007 and Excel 2007

1. In Word or Excel, click the Microsoft Office Button .



2. Click the ApplicationName Options button.


3. In the categories pane, click Add-ins.

4. In the Manage box, click Smart Tags, and then click Go.

5. Select the Label data with smart tags check box.




To enable smart tags in Word 2003 and Excel 2003

1. In Word or Excel, on the Tools menu, click AutoCorrect Options.

2. Click the Smart Tags tab.

3. Select the Label data with smart tags check box. Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon