• Home
  • About
  • BestBloggingIdeas
  • DotNetLearningSource
  • FORUM
  • Joblinks
  • Latest News
  • Policy
  • POSTS
  • SimplySqlServer.Com && SimplyAspDotNet.Com
  • Sitemap

Join Ours Forum

Asp.Net,C#,Ajax,Sql server,silverlight,Javascript codes exambles articles,Programming exambles

RSS Feed
  • Bounty Huge Roll [Amazon Frustration-Free Packaging]
  • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
  • The Basic Overview of Windows Mobile Development Asp.Net C#
  • Overview of Sql server extended properties Asp.Net C#
  • How to Use Sql Server Extended properties using visual studio Asp.Net C#
  • Adobe Dreamweaver Templates Accelerate Web Development
  • Top Tips for Web Design Projects
  • How to Achieve a Good Web Design Structure
  • To Use Or Not To Use Website Templates
  • Five Tips to a Successful Website
  • Top 10 Articles,


    Silverlight Datagrid Select Update Delete Insert Asp.Net C#

    Differences Similarities Benefits Between Typed Datasets and Untyped Datasets asp.net c#

    Linq to Sql Introduction Entities Ado.Net C# SqlClasses Attributes Linq Mapping

    Linq Programming/How Linq Works?/Linq Implementation In Asp.Net C# Ado.Net

    Performing Developing Using Investigating Asp.Net 2.0 Ajax Application Development Asp.Net C#

    Hosting/Install Wcf Services in a Windows Service Asp.Net C#

    Connecting Silverlight to Wcf Asp.Net C#

    Silverlight Data Grid Data Binding WCF Asp.Net C#

    Invoking/Accessing/Calling WCF Service Without Adding/Creating Proxy/Reference Asp.Net C#

    Performing Doing Creating Insert Update Delete sql data Using Linq Database Asp.Net C#

    c# Indexed Properties

    Posted by on January 22, 2010 Leave a comment (0) Go to comments

    Introduction:

    This tutorial shows how to implement a class that uses indexed properties. Indexed properties allow you to use a class that represents an array-like collection of several different kinds of things.

    Main:

    In this example the Document class is defined. Two indexed properties, Words and Characters, are used to perform some text operations on the Document object.

    using System;
     
    public class Document
    {
        // Type allowing the document to be viewed like an array of words:
        public class WordCollection
        {
            readonly Document document;  // The containing document
     
            internal WordCollection(Document d)
            {
               document = d;
            }
     
            // Helper function -- search character array "text", starting at
            // character "begin", for word number "wordCount." Returns false
            // if there are less than wordCount words. Sets "start" and
            // length" to the position and length of the word within text:
            private bool GetWord(char[] text, int begin, int wordCount, 
                                           out int start, out int length) 
            { 
                int end = text.Length;
                int count = 0;
                int inWord = -1;
                start = length = 0; 
     
                for (int i = begin; i <= end; ++i) 
                {
                    bool isLetter = i < end && Char.IsLetterOrDigit(text[i]);
     
                    if (inWord >= 0) 
                    {
                        if (!isLetter) 
                        {
                            if (count++ == wordCount) 
                            {
                                start = inWord;
                                length = i - inWord;
                                return true;
                            }
                            inWord = -1;
                        }
                    }
                    else 
                    {
                        if (isLetter)
                            inWord = i;
                    }
                }
                return false;
            }
     
            // Indexer to get and set words of the containing document:
            public string this[int index] 
            {
                get 
                { 
                    int start, length;
                    if (GetWord(document.TextArray, 0, index, out start, 
                                                              out length))
                        return new string(document.TextArray, start, length);
                    else
                        throw new IndexOutOfRangeException();
                }
                set 
                {
                    int start, length;
                    if (GetWord(document.TextArray, 0, index, out start, 
                                                             out length)) 
                    {
                        // Replace the word at start/length with the 
                        // string "value":
                        if (length == value.Length) 
                        {
                            Array.Copy(value.ToCharArray(), 0, 
                                     document.TextArray, start, length);
                        }
                        else 
                        {
                            char[] newText = 
                                new char[document.TextArray.Length + 
                                               value.Length - length];
                            Array.Copy(document.TextArray, 0, newText, 
                                                            0, start);
                            Array.Copy(value.ToCharArray(), 0, newText, 
                                                 start, value.Length);
                            Array.Copy(document.TextArray, start + length,
                                       newText, start + value.Length,
                                      document.TextArray.Length - start
                                                                - length);
                            document.TextArray = newText;
                        }
                    }                    
                    else
                        throw new IndexOutOfRangeException();
                }
            }
     
            // Get the count of words in the containing document:
            public int Count 
            {
                get 
                { 
                    int count = 0, start = 0, length = 0;
                    while (GetWord(document.TextArray, start + length, 0, 
                                                  out start, out length))
                        ++count;
                    return count; 
                }
            }
        }
     
        // Type allowing the document to be viewed like an "array" 
        // of characters:
        public class CharacterCollection
        {
            readonly Document document;  // The containing document
     
            internal CharacterCollection(Document d)
            {
              document = d; 
            }
     
            // Indexer to get and set characters in the containing document:
            public char this[int index] 
            {
                get 
                { 
                    return document.TextArray[index]; 
                }
                set 
                { 
                    document.TextArray[index] = value; 
                }
            }
     
            // Get the count of characters in the containing document:
            public int Count 
            {
                get 
                { 
                    return document.TextArray.Length; 
                }
            }
        }
     
        // Because the types of the fields have indexers, 
        // these fields appear as "indexed properties":
        public WordCollection Words;
        public CharacterCollection Characters;
     
        private char[] TextArray;  // The text of the document. 
     
        public Document(string initialText)
        {
            TextArray = initialText.ToCharArray();
            Words = new WordCollection(this);
            Characters = new CharacterCollection(this);
        }
     
        public string Text 
        {
            get 
            { 
               return new string(TextArray); 
            }
        }
    }
     
    class Test
    {
        static void Main()
        {
            Document d = new Document(
               "peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?"
            );
     
            // Change word "peter" to "penelope":
            for (int i = 0; i < d.Words.Count; ++i) 
            {
                if (d.Words[i] == "peter") 
                    d.Words[i] = "penelope";
            }
     
            // Change character "p" to "P"
            for (int i = 0; i < d.Characters.Count; ++i) 
            {
                if (d.Characters[i] == 'p')
                    d.Characters[i] = 'P';
            }
     
            Console.WriteLine(d.Text);
        }
    }

    Conclusion:

    Hope this helps,
    Happy Coding.

    References:

    msdn.microsoft.com

    C#
    ← C# Anonymous Methods
    DotNet Assemblies →

    Learn Easily Using Video Tutorials


    How to choose the right Java IDE – explained Eclipse NetBeans BlueJ

    Developing/Creating/Performing/Configuring Java Applications Using Eclipse IDE

    Step By Step Guide for Download/Install Configure Eclipse IDE for Java

    Editing data with the GridView control Asp.Net C#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Best way to prepare asp.net Interview - Success Stories

    Download Important Questions and PPT's:

    Sql Server Important Questions Online free download

    Dotnet Important Questions Online free download

    Exploring Linq to Sql Process Flow

    Learn how to perform silverlight programming

    Learn OOPs concepts in better and well manner

    Learn Ajax in better and well manner

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    *

    *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Enter your email address:

    Delivered by FeedBurner

    • Recent Posts

      • Bounty Huge Roll [Amazon Frustration-Free Packaging]
      • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
      • The Basic Overview of Windows Mobile Development Asp.Net C#
      • Overview of Sql server extended properties Asp.Net C#
      • How to Use Sql Server Extended properties using visual studio Asp.Net C#
    • Search by Tags!

      Application AspNet Basic between Black Bluetooth Build Business Collection Consultants Design Development Downloading effective Excel Experts Generics Implement Installing Interview Logic Management Microsoft Minutes Object Outlook Professional Programmer Programming Project Projects Questions Ready Select Server Services Silverlight Source Strings Studio Through using Visual Website Wordpress
    • Archives

      • August 2011
      • June 2011
      • May 2011
      • April 2011
      • March 2011
      • February 2011
      • December 2010
      • November 2010
      • October 2010
      • September 2010
      • August 2010
      • July 2010
      • June 2010
      • May 2010
      • April 2010
      • March 2010
      • February 2010
      • January 2010
      • December 2009
      • November 2009
      • October 2009
      • September 2009

    Copyright © 2012 NetProgrammingHelp.com

    Δ Top