Archive

Archive for the ‘.NET 4.0’ Category

Join Two Array – Zipping

February 18, 2013 Leave a comment

Last week I had a requirement where I need to Join two array,there was no relation that I could use to  join LINQ.

Int32[] iArrayOne = {12,34,45,89};

String[] sArrayTwo= { “Value1”,”value2”,”value3”,”Value4”}

Later which I posted a question in SO,in fact it was not possible directly in LINQ.Eric has written a Post that solves my requirement. Yes the operation is termed as Zipping. Until .NET 4.0 we need to use the below snippet to achieve it and starting 4.0 we will have that function as part of the .NET.

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>
(this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TSecond, TResult> resultSelector)
{
if (first == null) throw new ArgumentNullException("first");
if (second == null) throw new ArgumentNullException("second");
if (resultSelector == null) throw new ArgumentNullException("resultSelector");
return ZipIterator(first, second, resultSelector);
}

private static IEnumerable<TResult> ZipIterator<TFirst, TSecond, TResult>
(IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TSecond, TResult> resultSelector)
{
using (IEnumerator<TFirst> e1 = first.GetEnumerator())
using (IEnumerator<TSecond> e2 = second.GetEnumerator())
while (e1.MoveNext() && e2.MoveNext())
yield return resultSelector(e1.Current, e2.Current);
}
Zipping operation is defined as   var qry = from row in iArrayOne.Zip(sArrayTwo, (temp1, temp2) => 
new { ArrayObject1 = temp1, ArrayObject2 = temp2 })
Tags : ,
Advertisements
Categories: .NET 4.0, C# 4.0

ASP.NET 4.0 Snippet

June 21, 2009 1 comment

    This is new addition to the multi part post on upcoming .Net 4.0. Recently Microsoft has released the Beta 1 of .Net 4.0 and the Visual Studio 2010, you can get full update on this and how to download & install from this Video by Brian Keller.

    This post we will look in to one of the new option of Visual Studio 2010 for ASP.NET 4.0. We were using the code snippets in the visual studio for some time now, and with this release of the Visual Studio 2010 it has been expanded to the ASP.NET 4.0 and HTML as well. Earlier version it was limited to the C# and other languages only.

    The working of snippet is no different than the C# & other language codes, all the pre defined ASP.NET snippets are placed in the folder location C:\Program Files\Microsoft Visual Studio 10.0\Web\Snippets\HTML\1033\ASP.NET
and the HTML snippets reside in the folder C:\Program Files\Microsoft Visual Studio 10.0\Web\Snippets\HTML\1033\HTML . Below I have defined a snippet for GridView, once you defined the snippet, use the Tools-> Code Snippet Manager of Visual Studio and Import the Snippet and it will be available for the immediate use.


Snippet for the Grid View

Once Import just type in the Shortcut text for the snippet and hit Tab Tab. You snippet will be inserted to the code as below and you can change the Parameters that are defined in the snippet, in my case I have defined the parameter for ID and DataSourceID attribute.

Categories: ASP.NET 4.0

Optional Parameter, Named arguments & Default values C# 4.0

May 6, 2009 1 comment

One of the cool feature (list here) we always wanted is ability to provide optional parameters, default values for the methods. Microsoft has addressed this in C# 4.0 and definitely it will make to RTM as well. As there are many articles out in the web regarding this I will not reiterate it but give you a base understanding on this and let you to read the article for core understanding.

Following things are valid in C# 4.0

public Int32 DoAdd(Int32 iNumberOne,Int32 iNumberTwo=0,Int32 iNumberThree=0)

        {

            return iNumberOne + iNumberTwo+iNumberThree;

        }

            //value is passed to the First parameter

            DoAdd(1);           

            //value is passed to the first & second Parameter

            DoAdd(1,2);

            //Value is pased to the first,second & third parameter

            DoAdd(1, 2,3);

            //value is passed to the first and third parameter

            //this is where we can see one of the use of NamedParameters

            //calling this method this DoAdd(1,2) will men completly different

            //in this sence. Hence the Named Paramert helps here

            DoAdd(1, iNumberThree: 2);

            //When you use the Named Parameter order of the Parameter doent

            //matter

            DoAdd(iNumberThree: 2, iNumberOne: 2);

 

           

Points to Remember:

  • Optional Parameter should be declared just simply placing the default value with a = operator
  • Optional Parameter must appear after all required parameters.
  • ref or out parameter cannot have a default value.
  • Named parameters gives us the more code readability
  • Helps in the optional parameter methods

Named Parameters:

  • Named parameters gives us the more code readability
  • Helps in the optional parameter methods
  • Order of parameter, helps us to change the order of the Parameters while calling method and still have the ablity to map which value goes to which argument 

 

        public Int32 DoMath(Int32 iNumberOne,Boolean SqureTheNumber)

        {

            if (SqureTheNumber)

                return iNumberOne * iNumberOne;

            else

                return iNumberOne + 1;

        }

            //This doent give much information about the call

            DoMath(4, false);

 

            //This is more clear

            DoMath(4, SquareTheNumber:false);

 

 

 

Reference:

 

Categories: .NET 4.0, C# 4.0

.NET 4.0 – Part 1

April 21, 2009 2 comments

        You should be aware that .Net 4.0 is currently in CTP and I am currently in process of exploring the new features in .Net 4.0. Channel9 have a good series of videos that will help you to get started. This is where I started my journey towards .Net 4.0. I would like to share all the learning I had from the above videos and additions to that I would like to share few items that I noticed during my learning. Altogether you will find information about .Net 4.0, ASP.NET Visual Studio 2010. How well it integrates with SharePoint & more. To practice this you will need a CTP version of Visual Studio 2010 and .Net FX 4.0. You can download it from this location. Also be sure to follow these steps before you open the Visual Studio 2010 because it comes with a hardcoded value of 1 Jan 09 as expire date.

[Update: June 21,2009 : .Net 4.0 and Visual Studio 2010 Beta 1 is now avaliable, please refer to the post for more information.]

This article will host Table of Links for the all Upcoming posts.

    ASP.NET 4.0

    C# 4.0

   Visual Studio 2010

 

One feature that has everyone’s attention when it comes to ASP.NET 4.0 is ClientID. Till now we had the ClientID as the getter and renders the id based on the naming container, from 4.0 we will be able to decide how the client ID is render to the browser. Each of the server control in the ASP.NET 4.0 has three new Properties that decide how the client Id is rendered.

  1. ClientID         –    ID that we wish to give to the server control when rendered to the browser.
  2. ClientIDMode        –    Mode that decides how the client ID is generated. Can be anyone of the following value
  • Inherit         –    Gets the mode that is set to the parent.
  • Predictable    –    Gives us an option to predict the ClientID that is goingto get rendered. (In the case of data bound control it generates a auto increment number
  • Legacy        –    No change works as like the previous version and thisis the default value.
  • Static        –    Static value that we set will render as it is.

                         3. RowClientIDSuffix    –    Sometimes when we use the Predictable option for ClientIDMode we might need to have a different pattern of the value as the suffix rather than having auto increment. In such a case we can provide a Comma Separated Value of Data Fields (Off course this option is available for the data bound controls) so the client Id will have the value of the Data field appended to the suffix.

    Let’s see an Example of the above. I have a User Control (ClientIDUserControl.ascx) with a Label inside that, which is used in the page. You can see my project structure in the screen shot.

ClientIDMode:: Legacy : In the below picture you will see the even though I set the client ID of the Label control it renders client ID based on the naming container, because by default the ClientIDMode is Legacy, I didn’t specified the ClientIDMode attribute so it defaults to Legacy.

ClientIDMode:: Static : In the below picture you will see that both the client ID and ClientIDMode (Static) is set hence the client ID that is rendered to the client is same as the one we set in the Code.

 

ClientIDMode:: Inherit : In the below picture you will see that client id that was rendered is based on the Naming container because for the Label I set the ClientIDMode as Inherit. Because of this label check its parent (in this case it’s the User Control) for the ClientIDMode and as it doesn’t have one it goes to the Page and checks for the ClientIDMode, as page as well doesn’t have one, it further goes to the web config and check for the ClientIDMode and as it doesn’t have one it by defaults to the Legacy mode. You can also set the ClientIDMode property at the page or the WebApplication level.Refer to the notes section at the end of the article.

ClientIDMode:: Predictable : In the below picture you will see that ClientIDMode is set to Predictable and ClientID is set to “deleteEmployee” and as a result it renders the client id as a AutoIncremented value “DeleteEmployee_0” and soon. When setting the Predictable make sure that Parent control also has the ClientIDMode value set. Refer notes section in the end of the article.

ClientIDMode:Predictable and RowClientIDSuffix:

    When using the Predicatble option there is another tweak we can perform that is to generate our own predictable ID rather than using the auto incremented value of (0,1,2 …).In the below picture I have used RowClientIDSuffix to ID and Company, hence the rendered element has the ID in the format DeleteEmployee_[ID]_[Company]

 

Notes:

  1. While using the ClientIDMode=“Predictable” you need to set the ClientIDMode property in the parent control as well else the ClientID generated by the client control will not have the predictable value, it will default to Naming container method of generating the ID. In the above case I have set the ClientIDMode of the GridView to Static and the CheckBox which is the child control to Predictable.
  2. In addition to the setting the ClientIDMode to the each and every control you can set this value at the Page Level or at the Application level (Web.Config).When there is no explicitly set ClientIDMode to any of the control it will get the value from the page or Web.Config level if one is set else it default to the legacy mode. 
Mode ID ClientID Suffix Client ID At the Browser
Legacy/Nothing Set A C [ParentNamingContanierID]_A
Static A C C
Predictable A C C_0,C_1
Predictable A C ID C_[ID],C_[IDNext]