March 28, 2014

Dependency Injection - Design Patterns

Dependency Injection

Definition:

Dependency injection is a technique used in Oops. in this technique the component are loosely coupled by passing the dependencies to the object, rather than the object instantiating it own dependencies.

Explain in detail dependency injection?

March 15, 2014

Understanding Namespaces in c#




Understanding Namespace in C#

1000’s and 1000’s of classes are defined in .NET framework and all these classes are organized under different Namespaces. You can think of Namespaces as boxes with Names on it and all the classes relevant to the box are put in it – Under that namespace, so when a class is used we can refer to the class as namespace.class name.

In C# namespaces are used extensively - first while using the .NET classes, their respective namespaces are used and secondly while creating our own types – we are creating our own / user defined namespaces.




Having namespaces helps you to control the scope of the class and its methods. If there are no namespaces, we would not be able to use multiple classes having same name. Eg. we can have a class called Console having a method WriteLine under namespace MyNamespace – (MyNameSpace.Console.WriteLine - . I agree - it’s a bad idea to create such class and method. As we already know there’s a similar class in .NET framework under System Namespace. System.Console.WriteLine.

But in reality we might come across such situation where in we have same class name as a class in .Net framework or a third party dlls is having a same class name, in such cases namespaces help us to differentiate the classes and use them efficiently.

Note:  Visual Studio IDE helps us to organize the classes under different namespaces, by defaulting the project name as namespace of the class, but it is not mandatory. Namespaces do not refer to or correspond to a file or a directory.



Using namespaces:- A class can be invoked from a namespace using 3 ways.
  1.     Writing the fully qualified name ( see below example)
  2.     Implementing the using directive,
  3.     Implementing using alias ( see below example

Note: The above ways are used for calling classes for both .NET namespaces or from custom/ own namespaces.


The using directive allows us to use the classes / types without having to specify the namespace. 
The using alias allows us to create and identifier for a namespace or class. The identifier is assigned a fully qualified name. Ex using project = MyNamesapce.MyClassName;
Namespace alias qualifier:

The namespace alias qualifier (::) is used search for identifiers. It is used to reference a namespace alias and global:: us used to reference the global namespace. (Note: global is not an predefined alias qualifier, it gets this special status after its used with (::). Also you cannot used the global word with using alias (eg. Using global = MyNamespace.MyClass), this is a compiler error, because as mentioned earlier, global:: always reference the global namespace and not and alias.

Use of Global namespace alias:- 
global:: - is used to access the global namespace.
The global namespace access is required in scenarios where the user defined class / type has the same name as the system defined .NET class and the System defined class is now hidden from in the current namespace – See below example.
    



March 11, 2014

Open Visual Studio IDE from Run command of your choice ( when you have 2 different versions of Visual Studio)

Question:
When you have two versions of Visual Studio installed on the same machine, for example Visual Studio 2010 and Visual Studio 2012, you want to always open Visual Studio 2012 as default?

Solution: 
When you have two versions of Visual Studio installed, say first you installed Visual Studio 2010 and then installed Visual Studio 2012. If you are using shortcut to open Visual Studio IDE
Start - Run - devenv.exe
By default Visual Studio 2010 will be opened. As Visual Studio 2010 was installed first and a register entry was done.

Now that you have installed Visual Studio 2012 without uninstalling Visual Studio 2010, this registry entry is not made for Visual Studio 2012

You can make Visual Studio 2012 to open as default, by modifying the registry entry. See below the steps.
  • Click on Windows Start button.
  • Type regedit.exe in the search text box, regedit.exe will appear. 
  • Click on the regedit.exe. This will open the Registry Editor.
  • Navigate to the following path in the left section
    •  HKEY_LOCAL_MACHINE => SOFTWARE => Microsoft => Windows => CurrentVersion => App Paths => devenv.exe
  • Click on devenv.exe you will see the default key on the right section.
  • Double click on default to change the value.
  • In this case for Visual Studio 2010 the value would be  (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe)
  • Change this value to the path where Visual Studio 2012 devenv.exe is located, in this case (C:\Program Files(x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe) and click Ok.
  • Close Registry editor.
  • Now when you type devenv.exe from Run it is open Visual Studio 2012.
Following are default path where Visual Studio IDE is installed by default 

Visual Studio 2008
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe

Visual Studio 2010
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe 

Visual Studio 2012
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe 

Thanks,
Hussain Patel

March 5, 2014

How to: Set the Startup Form in a WPF Application

Question: In my WPF project, I have two .xaml WPF forms (MainWindow.xaml and Window1.xaml), how do I set Window1.xaml as a startup form for my WPFproject.?

Solution: 
  • Double click on the App.xaml file, in the Solution Explorer window.
  • App.xmal file would open and would have the following xml
<Application x:Class="WPFTutorialDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

You need to change the StartupUri attribute (highlighted yellow) with the name of the form you want to set as startup, in this case Window1.xaml, Save and close the file.

Thanks,
Hussain Patel