Windows Phone Developers

Saturday, April 14, 2012

How to change Text Color of Console (.NET/C#)

Configuring C# Console Application Title and Text Color


Console application should also look nice. There are ways to do it.

First - have a good title .. most often I have seen the Application Path etc as the title

The following snippet provides a way to customize the title of Console Application and also the TextColor

Console.Title = "Course Ware Example";
            System.Console.WriteLine("What you did on Red Letter Day");
            Console.ForegroundColor = ConsoleColor.Red ;
            string s1 = Console.ReadLine();


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

Saturday, March 17, 2012

C# IsDate Function / How to check valid date using C# function

IsDate Function in .NET

If you want to check if the given entry (string) is a valid date you could have used Microsoft.VisualBasic.IsDate Function in .NET. This function is available in VB.NET. For C# you can have a similar function

Here is a hint how to create that

private void IsDateExample()
        {
            //string s1 = DateTime.Now.ToString();
            bool bSuccess = false;
            DateTime d1;
            s1 = "17-Mar-20112";
            bSuccess = DateTime.TryParse(s1, out d1);
            if (bSuccess == true)
            {
                Console.WriteLine(d1.ToString());
            
            }
            else
            {
                Console.WriteLine("Not a date at all");
            }


 

        }




The function uses DateTime.TryParse to validate the given string 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, September 22, 2011

Threading in .NET/C# - How to get Thread Details

How to Execute a process in separate thread in VB.NET/C#

This was one of my favorites - Threading; but somehow I ignored for long. Now let us delve into it

Let us execute some small piece of code (a definite loop) through normal method and with the help of threading. Normal methid

private void ThreadExampleSync()
        {
            for (int iCnt = 0; iCnt < 3; iCnt++)
            {
                for (int j = 0; j < 100; j++)
                {

                    for (int j1 = 0; j1 < 9000000; j1++)
                    {

                    }
                }
                Console.WriteLine("Iteration {0} is executed by {1} at {2}", iCnt.ToString(), Thread.CurrentThread.ManagedThreadId.ToString(), DateTime.Now.ToString());
            }
        }
When we move to threading let us take the looping part into a separate method and call the method using different Threads
private void ThreadExampleAsync()
        {
            for (int iCnt = 0; iCnt < 3; iCnt++)
            {
                var t1 = new Thread(ExecuteTasks);
                t1.Start(iCnt);
            }
        }


        private  static void ExecuteTasks(object iInput)
        {
            for (int j = 0; j < 100; j++)
            {

                for (int j1 = 0; j1 < 9000000; j1++)
                {
                    //nothing

                }
            }
            Console.WriteLine("Iteration {0} is executed by {1} at {2}", iInput.ToString(), Thread.CurrentThread.ManagedThreadId.ToString(), DateTime.Now.ToString());
        }

Since we are calling new thread for each iteration - ExecuteTasks gets executed by different threads as shown below

The time taken when we use different threads is faster than the synchronous way. However, care must be taken in Threading to avoid excess memory usage/leakages 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

Tuesday, September 20, 2011

Argument 1: cannot convert from 'method group' to 'System.Threading.ThreadStart'

The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ThreadStart)' has some invalid arguments

These errors occur when the Method that is called has parameters other than object. The compiler expects the signature to take object

var t1 = new Thread(ExecuteTasks);

will throw an error if the method signature is like this

private  static void ExecuteTasks(int iInput)

The following one is acceptable

private  static void ExecuteTasks(object iInput)
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, September 16, 2011

VB.NET / C# - How to Check if OS is 64-Bit or 32-Bit

The following snippet gives a hint :

        private void GetOSConfig()
        {
            string sProcess;
            if (Environment.Is64BitOperatingSystem  == true )
            {
                sProcess = "64 Bit";
            }
            {
                sProcess = "32 Bit";
            }
            Console.WriteLine("OS is {0}", sProcess  );
        } 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

How to Pass Multiple/Variable Parameters to a Function/Method in C#/.NET

C#/VB.NET Function with dynamic parameters / How to make a function accept infinite number of arguments

Normally a method has a definite number of parameters and they are passed from the calling method. What if you are not sure of the number of parameters or want to be flexible in that. A Simple example can be a Adding machine - which adds any numbers sent .

It is possible to achieve that with params keyword -

private void AddAsManyNumbersAsUCan(params int[] numbers)

{

int iTotal = 0;

foreach (int i1 in numbers)

{

iTotal += i1;

}

Console.WriteLine("Sum of {0} numbers added to {1}",numbers.Length ,iTotal);

}

The above method accepts any number of arguments (provided they are of the same type). The method can be called like:

AddAsManyNumbersAsUCan();

AddAsManyNumbersAsUCan(1,2,3);

AddAsManyNumbersAsUCan(100,-1,23,2121,4562);
Will give the following output




You can change the type of the parameter to a generic one - List etc to have a broader use 
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, September 15, 2011

VB.NET FindControl in Windows forms application

How to loop through windows form controls by name (C# / VB.NET)

ASP.NET dev have the advantage of FindControl, which does this work quite easily. For Windows forms developers the need to loop through the controls using ForEach in the control collection or can use the Find method as shown below:

foreach (Control ctl in Controls.Find("textBox" + i, false))

{

string stext = ctl.Text ;

};


Here we have used ControlCollection Controls.Find method to refer a Textbox and get the value 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