Windows Phone Developers

Saturday, May 8, 2010

How to format Currency (Pound/Dollar/Euro etc) using C#/VB.NET/Csharp Writeline function

Formatting Currency in C#/VB.NET/Csharp Writeline function

You can use .NET's Composite Formatting to format the currency as shown below

    Double Incentive = 234.45;
            Console.WriteLine("The agreed incentive in local currency is {0:C} ", Incentive);
        

If you want to convert it into dollars/pounds etc you need to change the culture info :

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
            Console.WriteLine("The agreed incentive in USD is {0:C} ", Incentive);


            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB", false);
            Console.WriteLine("The agreed incentive in GBP is {0:C} ", Incentive);




The above code needs the following directives

using System.Threading;
using System.Globalization;
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

en-UK is an invalid culture identifier - VB.NET/C#/Csharp

Culture is not supported. Parameter name: name en-UK is an invalid culture identifier.


We are used to en-UK as culture. but for some reasons MS doesn't support en-UK (UK is used for Ukraine)

Hence replace
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-UK", false);
            

with

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB", 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

Friday, May 7, 2010

How to pass an argument by reference in Csharp/C#/VB.NET

Pass by Reference/Pass by Value in VB.NET/C-Sharp/C#

Here is an example of passing an argument to a function by Value:

private static void PassByValueExample(double Salary)//
        {
            Salary = Salary * 1.2;
        }


Here is how it is passed as reference
private static void PassByRefExample(ref double Salary)//
        {
            Salary = Salary * 1.2;
        }


The following code shows the difference between the two

Console.WriteLine("Salary before hike is {0:F}", Sal);
            PassByValueExample( Sal);
            Console.WriteLine("Salary after hike is {0:F}", Sal);

            Console.WriteLine("Salary before hike is {0:F}", Sal);
            PassByRefExample(ref Sal);
            Console.WriteLine("Salary after hike is {0:F}", Sal);






See also:


Submit your website to 20 Search Engines - FREE with ineedhits!




Search Engine Optimization and SEO Tools




Software


webbhotell 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

Input string was not in a correct format. - Csharp/VB.NET WriteLine Function

If you are using VB.NET/C# Writeline function and facing the following error, it has to do with formatting


Wrong :
double Sal = 1250.45;
        Console.WriteLine("Salary before hike is {0:D}", Sal); //Decimal instead of double
        Console.WriteLine("Salary before hike is {F}", Sal); //Index is missing
            

Correct:
double Sal = 1250.45;
        
Console.WriteLine("Salary before hike is {0:F}", Sal);


USe appropriate composite format string - decimal , double 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