Monday, September 30, 2013

Simple example of IEnumerable and IEnumerator in C#

Here is a simplest example of using IEnumerable and IEnumerator in C#. Since 'String' is an IEnumerable object, so we can enumerate it using IEnumerable and IEnumerator. 'String' object is just chosen for sake of simplicity. The way we are using "IEnumerable and IEnumerator" here can be extended to any IEnumerable object (such as List<T>, Dictionary<TK, TV>, Hashset, SortedSet etc)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
 
namespace IEnumerableDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //The following example illustrates low-level use of IEnumerable and IEnumerator:
            string s = "How to use IEnumerable and IEnumerator";
            
            // Because string implements IEnumerable, we can call GetEnumerator():
            IEnumerator rator = s.GetEnumerator();
            
            // Iterating entire collection one by one until it reaches at the end
            while (rator.MoveNext())
            {
                // Getting the current object and cast it into its corresponding object with which
                // the IEnumerable collection is made (For example: String collection is made with char object)
 
                char curObject = (char)rator.Current;
                Console.Write(curObject + ".");
            }
           
            Console.Read();
        }
    }
}


Feel free to put your comments and concern if any.

Thanks,
Hemant

Tuesday, September 17, 2013

Reading paragraph from a MS Word document using C#


Here I am going to share how we can read paragraphs from a Microsoft Word Documnet using Microsoft.Office.Interop.Word DLL.

(1) Add reference of Microsoft.Office.Interop.Word DLL into your project
(2) With the help of following class, we can read a specific paragraph of a MS Word Document.


   class WordDocReader
    {
        //To read paragraph contents of a Word document using Microsoft.Office.Interop.Word DLL
        public  string ReadFileContent(string path, int paraGraphNum)
        {
            int i = 0;
 
            StringBuilder sb = new StringBuilder();
 
            Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
 
            object file = path;
 
            object nullobj = System.Reflection.Missing.Value;
 
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open
                                                    (ref file, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj);
 
 
            Microsoft.Office.Interop.Word.Paragraphs DocPar = doc.Paragraphs;
 
            // Count number of paragraphs in the file
            long parCount = DocPar.Count;
 
            // Step through the paragraphs
            while (i < parCount)
            {
                i++;
                if (i == paraGraphNum)
                {
                    sb.Append(DocPar[i].Range.Text);
                    break;
                }
            }
 
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
            return(sb.ToString());
        }
    }