Thursday, January 7, 2010

How to use Reserved Words in .NET (C#)

If you can use tens and thousands of words as identifier, why should one use the few keywords reserved by C#?
private static void KeywordExample()
{ 
string string = "Some String";
}
Might be you still want to use ‘bool’, ‘string’ etc. But using this will throw “identifier expected, 'keyword' is a keyword - Compiler Error CS1041”
A reserved word for the C# language was found where an identifier was expected. Replace the keyword with a user-specified identifier.

To overcome this error, prefix the identifier with “@”. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.
private static void KeywordExample()
{ 
string @string = "Some String";
Console.Write(@string);
}

Check Multiple Instances of an Application using C#

Simple code to check if multiple instances of current application are running in the machine using System.Diagnostics
        private static void check_application_process_instances()
        {
            Process[] oProcess;
            String sModuleName;
            String sProcessName;
            sModuleName = Process.GetCurrentProcess().MainModule.ModuleName;
            sProcessName = System.IO.Path.GetFileNameWithoutExtension(sModuleName);
            oProcess = Process.GetProcessesByName(sProcessName);
            if (oProcess.Length > 1)
            {
                MessageBox.Show("More than one instance is running!");
            }
        }

Tie in Max or Min Function - WITH TIES

If you have two employees with the same birth date - the youngest/oldest employee has a tie. Then use of WITH TIES clause will solve it.

If WITH TIES is also specified, all rows that contain the last value returned by the ORDER BY clause are returned, even if doing this exceeds the number specified by expression.

Top 1 could return two or even more rows in case of a Tie

Select TOP 1 WITH TIES BirthDate from HumanResources.Employee Order By 1 Desc

WITH TIES can be used to get the matching records of the last/first even if it is more than the given number or percent

WITH TIES requires an ORDER BY clause.

How to Convert CSV to XML using C#

Language Integrated Query (LINQ) can be used to convert a CSV file to XML. Here is the sample CSV file that needs to be converted

The following code reads the CSV file to an array. LINQ is used to loop through the array and the contents are written as XML using XElement (System.XML.Linq).

public void ConvertCSVToXML()
        {
            String[] FileContent = System.IO.File.ReadAllLines(@"C:\Temp\vba.csv");
            String XMLNS = "";
            XElement Inv = new XElement("Invoice",
                from items in FileContent
                let fields = items.Split(',')
                select new XElement("Item",
                    new XElement("ID", fields[0]),
                    new XElement("Name", fields[1]),
                    new XElement("Price", fields[2]),
                    new XElement("Availability", fields[3]),
                    new XElement("TotalPrice", fields[4])
                    )
                );
            System.IO.File.WriteAllText(@"C:\Temp\vba.xml", XMLNS + Inv.ToString());
        }