Pages

Monday, 26 March 2012

Anonymous Types implementation in VB.NET

Anonymous types were designed to provide greater flexibility to create your own type at run time with a simple keyword {New} with as many properties you need, there is no limit in declaring properties with different types to create an anonymous type.

How to create an anonymous type?

Create an object and declare properties that you require, similar to the below mentioned code. After that .NET compiler will take care of everything.

New With {.FirstName = Robert, _
.MiddleName = De, _
                    .LastName = niro, _
                    .Age =27
                     }

What .NET compiler does?

It generates a sealed class similar to the anonymous type declared and it makes the type as immutable and properties declared will be read-only.

Advantages of using Anonymous type:


  • Compiler creates the code on fly and I believe compiler creates a better code than any developer.
  • Reduce in number of code files that holds properties and need not bother about maintenance.

Caveats in using anonymous type:


  • Scope of an anonymous type will be specific to the implemented member
  • Anonymous type neither can be used as parameters nor used as return types
  • Cannot be generated in designer files [HLD/DSD] as separate entity

HOW TO RETURN AN ANONYMOUS TYPE?

It’s really great that we can return an anonymous type and direct cast the object with the original type, then you can access the properties in the instead of using reflection. Kudos to Type inference in VB.NET which makes developer life easier with one setting {Option Infer On}, this helps you to get the actual type from calling methods without the mentioning the actual type name.

Below is the sample that return an anonymous type as object and in the caller class how we type cast using type inference,

Declare a member the returns an object as mentioned below, in the calling class

Public Function GetCustomerDetails() As Object
Return New With {.FirstName = Robert, _
                        .MiddleName = Sr, _
                    .LastName = Deniro, _
                    .Age =27
                     }
End Function
In the caller class
Dim customerDetail = CastCustomerDetail(GetCustomerDetails(), _
                                               New With {                       .FirstName = String.Empty, _
                                                                                                .MiddleName = String.Empty, _
                                                                                                               .LastName = String.Empty, _
                                                                                      .Age = 0
                                                    })

Private Function CastCustomerDetail (Of T)(customerDetail As Object, customerDetailType As T) As T
        Return DirectCast(customerDetail, T)
End Function
Now in the customerDetail variable you can access the properties of {.FirstName as Robert and .LastName as Deniro}

Caveats:

Anonymous types cannot be identified across assemblies with same type and can be realized at runtime and not in compile time {we will discuss this in the below section}

Tuesday, 8 November 2011

Installation of windows 8 developer preview


With lot of enthusiasm started installation of windows8[32-bit] in physical box instead running the image in the virtual box.

System Configuration,
Core 2 DUO
4GB RAM
1.5TB HDD
No Graphics card*, no touch screen monitor*
Current box is running with dual OS {Windows server 2003 and Windows7}

How did I proceed with installation of Windows8?
Downloaded the WIN8 developer preview , downloaded version will be in {.ISO} format.
First hurdle to my installation, system BIOS cannot read boot files from ISO.
What next? To make this as an new installable version we need to extract the image file, one more stop How to extract? After a quick Google, got a very good tool to extract the Image files downloaded from EXTRACTNOW.

Extracted the 2.82GB .ISO file and got 2.88GB file list along with bootable files. Loaded my USB memory drive with win8 installable files and restarted my PC by the changing the BIOS settings to USB enabled Boot. No clue why my BIOS didn't boot the windows8 for installation?...Its a perfect disaster with all the above stuff.

But still I have some hope in creating a new installation instance of Win8 in my box by installing from different OS. Logged in to windows server2003 OS and executed the win8 setup.exe file, upgrade of OS was in disable mode and I preferred custom installation with new instance of windows8 in a different drive. Setup was completed approximately with in 15 minutes, eagerly waiting to check the performance and navigations in windows8.

What's new? Awesome all I could see a new screen to select the OS after the restart. Screen really surprised me, I was not able to find instance of windows server 2003 OS in the selection list. I didn't really bothered about my missing windows server2003 OS why because I got windows8 in my box.

Selected the WIN8 developer preview OS and booted win8 for the first time in my box. Able to see all the metro style apps but not able to run the metro style apps, again a problem but able to find it quick.
Workaround!, changed the resolution to 1024*768. Assuming this is the lowest resolution to run metro apps.

Observations, when you select the OS from the boot up screen, surprised to see that it again detect the mother board and hard ware settings to boot the OS. I guess this is to find the touch enabled devices connected to the box.

Monday, 15 August 2011

Microsoft [AIO] Code Framework


Ever growing list of new technologies, and increased search time for a right code snippet and above all, authenticity of the time-tested code, these some of the daily happenings in a developer‟s world. Looks like Microsoft is listening to its developers, closely. Here is one mine of source code which gives every code implementation that was introduced in Microsoft.

Microsoft offers a comprehensive Framework called [All-In-One] Framework also known as [One Code], which makes technical developers lives easier in implementing simple/complex coding methodologies by accessing the centralized code repository maintained by Microsoft.

Best part of all [All-In-One] Framework team also offers a sample code request service, which gives you the sample code based on the requirement that requested for.


How to use [All-In-One] Framework?
We have two options to access the centralized code sample repository,
1. Download [All-In-One] Framework, install in your local machine as a click-once application and run the [All-In-One] framework sample browser from your installed location.

  • Browser will display all the code samples based on the search criteria
  • It almost covers the Microsoft technologies that any developer would require
  • Also, you can download the source for free

2. Can also be configure with visual studio as a plug-in using [Extension-Manager], it‟s an add-in configuration

  • Select [Search Code sample] will redirect you the [All-In-One] framework window in VS which can be docked in IDE.
  • Also we have an option based on the specific API that you want as search criteria by simply selecting the member and click [Search Code Sample]

Quick walk-through:

For instance, if a developer got a requirement to develop an application using MVC.
Instead of making his own practices by learning, here is [All-In-One] Framework which gives you entire code implementation with Microsoft standards in just one click.

VS: Select Tools --> Search Code 

Sample

  • Enter {MVC} in the search textbox in the below snapshot
  • Select the languages/Technology you require for development
  • Click [Search] button
  • There you go all the code samples against the search criteria will be displayed and a new tab will be created on every search criteria {e.g. MVC/MVC VB in the below snapshot}
 There you go all the code samples against the search criteria will be displayed and a new tab will be created on every search criteria {e.g. MVC/MVC VB in the below snapshot}

[All-Izz-Well] for developers after [All-In-One]………Happy Programming…Cheers