Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use SortedList.Item Property

How to use SortedList.Item Property

Scheduled Pinned Locked Moved C#
tutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 956892
    wrote on last edited by
    #1

    Hi, Can somebody please tell me how to use the "Item" property in System.Collections.SortedList class. I wanted to get and set the value field depending on the key. Thanks Raj

    S H 2 Replies Last reply
    0
    • U User 956892

      Hi, Can somebody please tell me how to use the "Item" property in System.Collections.SortedList class. I wanted to get and set the value field depending on the key. Thanks Raj

      S Offline
      S Offline
      Spanky3
      wrote on last edited by
      #2

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionssortedlistclasstopic.asp[^] Hope that helps, seems pretty straight forward :-D

      1 Reply Last reply
      0
      • U User 956892

        Hi, Can somebody please tell me how to use the "Item" property in System.Collections.SortedList class. I wanted to get and set the value field depending on the key. Thanks Raj

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        If you look at the documentation for the Items property, it says that it's the indexer in C# (the same as other lists' Items properties). This means you use it like this:

        myList[key] = value;
        value = myList[key];

        You can use a key because a SortedList implement IDictionary, an interface for key/value collections. Mosts lists in the .NET FCL simply take an index (0-based, as with all .NET languages) in their indexer.

        Microsoft MVP, Visual C# My Articles

        U 1 Reply Last reply
        0
        • H Heath Stewart

          If you look at the documentation for the Items property, it says that it's the indexer in C# (the same as other lists' Items properties). This means you use it like this:

          myList[key] = value;
          value = myList[key];

          You can use a key because a SortedList implement IDictionary, an interface for key/value collections. Mosts lists in the .NET FCL simply take an index (0-based, as with all .NET languages) in their indexer.

          Microsoft MVP, Visual C# My Articles

          U Offline
          U Offline
          User 956892
          wrote on last edited by
          #4

          Thanks for your reply. I am new to .Net. Trying to understand these properties. DO I have to override this property. I am doing like this, but it doesn't work. In my Sorted list keys are of type string and value is of type int. public override int Item(string myKey) { get { return mySrotedList[myKey]; } set { mySortedList[myKey] = value; } } Thanks for your help and time. Raj

          H 1 Reply Last reply
          0
          • U User 956892

            Thanks for your reply. I am new to .Net. Trying to understand these properties. DO I have to override this property. I am doing like this, but it doesn't work. In my Sorted list keys are of type string and value is of type int. public override int Item(string myKey) { get { return mySrotedList[myKey]; } set { mySortedList[myKey] = value; } } Thanks for your help and time. Raj

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            Why are you trying to override this? If you're just using a SortedList, you just use it like the code example I gave. If you do want to extend a class, you only have to override abstract methods, and override other methods and properties as you need to. In this case, though, it's seems like you don't need to. Besides, when you declare an indexer for a class in C#, you use the following syntax:

            public object this[int index]
            {
            get { return internalList[index]; }
            set { internalList[index] = value; }
            }

            Read the .NET Framework SDK for more information, especially since your new. Specifically, see the Visual C# Language[^] topic.

            Microsoft MVP, Visual C# My Articles

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups