Friday, November 15, 2013

Split comma separated entries to new rows in Excel

How to convert the below data in a Excel sheet
Col A | Col B
1     | angry birds, gaming
2     | nirvana, rock, band
What I want to do is split the comma separated entries in the second column and insert in new rows like below:
Col A | Col B
1     | angry birds
1     | gaming
2     | nirvana
2     | rock
2     | band
This can be done with VBA. There is excellent solution provided on stackoverflow here

brettdj's code was very helpful! The extended code by Kolath to add a user input to select the range to transform, the target column, and the delimeter. He set this up tied to a keyboard macro so that it could do this to a bunch of different columns. Code credit goes to both of them.


Below is the Macro to be used
Sub SliceNDice()
Dim objRegex
Dim inputRange
Dim outputRange
Dim lngRow As Long
Dim lngCnt As Long
Dim tempArr() As String
Dim strArr
Dim outputCell
Dim delimeter As String
Set objRegex = CreateObject("vbscript.regexp")
objRegex.Pattern = "^\s+(.+?)"

'Get input range from the user
Set inputRange = Application.InputBox(Prompt:="Please Select Range", Title:="Range Select", Type:=8)
Set outputCell = Application.InputBox(Prompt:="Please Select output range", Title:="Output Select", Type:=8)
delimeter = Application.InputBox(Prompt:="Please Select delimeter", Title:="Delimeter Select")

'Define the range to be analysed
inputRange = Range([a1], Cells(Rows.Count, "b").End(xlUp)).Value2
ReDim outputRange(1 To 2, 1 To 1000)
For lngRow = 1 To UBound(inputRange, 1)

'Split each string by ","
tempArr = Split(inputRange(lngRow, 2), delimeter)
For Each strArr In tempArr lngCnt
    lngCnt = lngCnt + 1

    'Add another 1000 records to resorted array every 1000 records
    If lngCnt Mod 1000 = 0 Then ReDim Preserve outputRange(1 To 2, 1 To lngCnt + 1000)
    
    outputRange(1, lngCnt) = inputRange(lngRow, 1)
    outputRange(2, lngCnt) = objRegex.Replace(strArr, "$1")
Next
Next lngRow

'Dump the re-ordered range to target columns
outputCell.Resize(lngCnt, 2).Value2 = Application.Transpose(outputRange)
 End Sub

Be sure to have the data in the same format as provided in sample. Also while selecting the input range select the first set of row and in output range only the first set of columns. This will also add header row by default.







Thursday, March 14, 2013

C# Type Inference In Generic Methods

As you may already know, C# compiler don’t support type inference for generic classes, but it supports type inference for generic methods. To illustrate the point, let us consider a very simple example
class Program
    {
        //Our generic IsGreaterThan method
        public static bool IsGreaterThan<T>(T x, T y) where T : IComparable<T>
        {
            return (x.CompareTo(y) > 0);
        }

        static void Main(string[] args)
        {
            //You don't need to explicitly specify IsGreaterThan<int>
            var result = IsGreaterThan(20,10);
            Console.WriteLine(result); 
        }
 }


As you can see, we are not specifying the type explicitly, while calling our IsGreaterThan method - it is inferred automatically by the compiler. That is simple, isn’t it? Now, things become a a bit more interesting when you combine Generic method type inference with extension methods. Have a look at this code, we just made our IsGreaterThan<T> method an extension method.

public static class UtilExtensions
    {
        //Our generic IsGreaterThan extension method
        public static bool IsGreaterThan<T>(this T x, T y) where T : IComparable<T>
        {
            return (x.CompareTo(y) > 0);
        }      
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            //You still don't need to explicitly specify IsGreaterThan<int>
            var result = 20.IsGreaterThan(10);
            Console.WriteLine(result);

        }

    }

As you can see, we are still good - the compiler can still infer the types. And of course, you can leverage our IsGreaterThan against any IComparable implementation.
Ref: http://www.amazedsaint.com/2010/10/c-type-inference-in-generic-methods.html

Monday, February 25, 2013

WCF Concurrency (Single, Multiple, and Reentrant) and Throttling


In this article SK (Shivprasad Koirala) has explained, what is WCF concurrency and the three important types of WCF concurrency. Click Here

Three ways to do WCF instance management

Very often we would like to control the way WCF service objects are instantiated on a WCF server. You would want to control how long the WCF instances should be residing on the server.
The WCF framework has provided three ways by which we can control WCF instance creation. In this article, we will first try to understand those three ways of WCF service instance control with simple code samples of how to achieve them. Finally, we will compare when to use them and under what situations.
A must read article, for understanding the WCF Instance Management Click here

Monday, February 18, 2013

Difference between WCF and Web service

Web service is a part of WCF. WCF offers much more flexibility and portability to develop a service when comparing to web service. Still we are having more advantages over Web service, following table provides detailed difference between them.
Features Web Service WCF
Hosting It can be hosted in IIS It can be hosted in IIS, windows activation service, Self-hosting, Windows service
Programming [WebService] attribute has to be added to the class [ServiceContraact] attribute has to be added to the class
Model [WebMethod] attribute represents the method exposed to client [OperationContract] attribute represents the method exposed to client
Operation One-way, Request- Response are the different operations supported in web service One-Way, Request-Response, Duplex are different type of operations supported in WCF
XML System.Xml.serialization name space is used for serialization System.Runtime.Serialization namespace is used for serialization
Encoding XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom XML 1.0, MTOM, Binary, Custom
Transports Can be accessed through HTTP, TCP, Custom Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Protocols Security Security, Reliable messaging, Transactions