Wednesday, April 9, 2014

How to change width of “Solution Configurations” drop-down in VS2012 IDE?

Many Visual Studio users face issue when “Solution Configuration” value exceeds the width of the drop down control. User can see only truncated value.
In my case I had Solution Configuration values as “DebugProj_x64” and “DebugProj_x86” but in VS 2012 these values were shown as “DebugProj” only. So there was no way to identify, from UI itself, whether selected config is x64 or x86.

Here is a way to increase the size of “Solution Configuration” drop down in VS 2012.

Step 1:
Go to Tools menu >> Customize



Step 2:
Select Command Tab >> Select Toolbar radio button >> Standard >> Solution Configuration >> Click modify selection >> change value of Width here.


Wednesday, March 12, 2014

Object vs Var vs Dynamic

Object: 
Each object in C# is derived from object type, either directly or indirectly. It is compile time variable and require boxing and unboxing for conversion and it makes it slow. You can change value type to reference type and vice versa. 

        public void CheckObject()
        {
            object test = 10;
            test = test + 10;    // Compile time error
            test = "hello";      // No error, Boxing happens here
        }

Var: 
It is compile time variable and does not require boxing and unboxing. Since Var is a compile time feature, all type checking is done at compile time only. Once Var has been initialized, you can't change type stored in it.

        public void CheckVar()
        {
            var test = 10;         // after this line test has become of integer type
            test = test + 10;    // No error
            test = "hello";        // Compile time error as test is an integer type
        }


Dynamic: 
It is run time variable and not require boxing and unboxing. You can assign and value to dynamic and also can change value type stored in same. All errors on dynamic can be discovered at run time only. We an also say that dynamic is a run time object which can hold any type of data.


        public void CheckDynamic()
        {
            dynamic test = 10;
            test = test + 10;    // No error
            test = "hello";       // No error, neither compile time nor run time
        }

So here, All these variables are handy and can be used on the basis of our needs. Var is mainly used in case of LINQ queries and dynamic is used mainly in COM interop cases. I used dynamic mainly for COM interaction and as a replacement of Reflection.

Wednesday, February 26, 2014

Parallel.For loop vs simple for loop

Here is the quick intro to Parallel.For loop to enable data parallelism and of-course benefits of same.

Parallel.For can be used when we can execute for loop iterations in parallel. Here source collection is partitioned and threads are assigned to execute actions.

Example:

Data:

string[] strArraly = this.textBox1.Text.Split(' ');  // textbox1 has a string of approx. 400 words.
int[] lenArray = new int[strArraly.Length];

For Loop:

     for (int i = 0; i < strArraly.Length; i++)
     {
         lenArray[i] = GetStringLengh(strArraly[i]);
     }

Parallel.For

     Parallel.For(0, strArraly.Length, i =>
     {
          lenArray[i] = GetStringLengh(strArraly[i]);
     }
     );

Time consuming Method:

       private int GetStringLengh(string str)
      {
          Thread.Sleep(100);
          return str.Length;
      }


Here if we check the time taken by for loop and Parallel.For loop, there is huge difference in both. For is taking around 38 seconds while Parallel For is taking only 6 second to execute all iterations. Thus Parallel.For can be very handy in some situations.


Tuesday, February 25, 2014

How to get OS version?

Here are steps to get OS version using C#.net code
Code line to get installed OS version string:
OperatingSystem OsInfo = Environment.OSVersion;

Steps to get Windows 95, Windows 98, Windows Me and Windows 98 Second Edition

case PlatformID.Win32Windows:
                    {
                        switch (osInfo.Version.Minor)
                        {
                            case 0:
                                {
                                    osName = "Windows 95";
                                    break;
                                }

                            case 10:
                                {
                                    if (osInfo.Version.Revision.ToString() == "2222A")
                                    {
                                        osName = "Windows 98 Second Edition";
                                    }
                                    else
                                    {
                                        osName = "Windows 98";
                                    }
                                    break;
                                }

                            case 90:
                                {
                                    osName = "Windows Me";
                                    break;
                                }
                        }
                        break;
                    }

For other versions, like XL, Windows7 and Windows8

case PlatformID.Win32NT:
                    {
                        switch (osInfo.Version.Major)
                        {
                            case 3:
                                {
                                    osName = "Windows NT 3.51";
                                    break;
                                }

                            case 4:
                                {
                                    osName = "Windows NT 4.0";
                                    break;
                                }

                            case 5:
                                {
                                    if (osInfo.Version.Minor == 0)
                                    {
                                        osName = "Windows 2000";
                                    }
                                    else if (osInfo.Version.Minor == 1)
                                    {
                                        osName = "Windows XP";
                                    }
                                    else if (osInfo.Version.Minor == 2)
                                    {
                                        osName = "Windows Server 2003";
                                    }
                                    break;
                                }

                            case 6:
                                if (osInfo.Version.Minor == 0)
                                {
                                    osName = "Windows Vista";           //6.0.x (Windows Vista)
                                }
                                else if (osInfo.Version.Minor == 1)
                                {
                                    osName = "Windows7";                //6.1.x (Windows 7 and above)
                                }
                                else
                                {
                                    osName = "Windows8";                //6.2.x (Windows 8 and above)
                                }
                                break;
                        }
                        break;

                    }

How to get installed .Net Framework version?

Here is the way to find out the installed version of .Net framework on your machine.

Manual Way
1.    Start run >> Regedit
2.  Go to “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\”
3.  Check keys under this. See image below for help.




C#.Net Code
Make sure to include following namespace in your class

       using Microsoft.Win32;


Method to get registry value

       public String GetVersion()
        {
            RegistryKey mobjSubRegistryKey;
            String strVersionDesc = "Installed: ";
            try
            {
                RegistryKey HKLM_Key = Registry.LocalMachine;
                mobjSubRegistryKey = HKLM_Key.OpenSubKey(
@"SOFTWARE\Microsoft\NET Framework Setup\NDP\");

                if (mobjSubRegistryKey == null)
                {
                    return "Not Installed";
                }

                String[] strarray = mobjSubRegistryKey.GetSubKeyNames();
               
                if (strarray == null)
                {
                    return "Not Installed";
                }

                foreach (String str in strarray)
                {
                    if (str == "v1.1.4322")
                    {
                        strVersionDesc += "[.Net Framework 1.1] ";
                    }
                    else if (str == "v2.0.50727")
                    {
                        strVersionDesc += "[.Net Framework 2.0] ";
                    }
                    else if (str == "v3.0")
                    {
                        strVersionDesc += "[.Net Framework 3.0] ";
                    }
                    else if (str == "v3.5")
                    {
                        strVersionDesc += "[.Net Framework 3.5] ";
                    }
                    else if (str == "v4")
                    {
                        strVersionDesc += "[.Net Framework 4.0] ";
                    }
                }

                return strVersionDesc;
            }
            catch(Exception ex)
            {
                return “An Error has occurred while capturing info”;
            }
        }