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 get single item in foreach loop ??

how to get single item in foreach loop ??

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 6 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.
  • S Offline
    S Offline
    sikandarhayat
    wrote on last edited by
    #1

    how to get single item in foreach loop ?? here is the sample foreach (clsLabels lbl in m_clsLabelCollection) { lstlblname.Items.Add(lbl.label); lstlblvalue.Items.Add(lbl.Value); lstcustext.Items.Add(lbl.customValue); } i want a perticular lable from the clsLabelCollection(collection class inherited from CollectionBase) class clsLabels constructor added to clsLabelCollectionclass clsLabels(string,string,string)

    sikandar

    M C E S 4 Replies Last reply
    0
    • S sikandarhayat

      how to get single item in foreach loop ?? here is the sample foreach (clsLabels lbl in m_clsLabelCollection) { lstlblname.Items.Add(lbl.label); lstlblvalue.Items.Add(lbl.Value); lstcustext.Items.Add(lbl.customValue); } i want a perticular lable from the clsLabelCollection(collection class inherited from CollectionBase) class clsLabels constructor added to clsLabelCollectionclass clsLabels(string,string,string)

      sikandar

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      sikandarhayat wrote:

      i want a perticular lable

      Whats different on this label?

      S 1 Reply Last reply
      0
      • M Martin 0

        sikandarhayat wrote:

        i want a perticular lable

        Whats different on this label?

        S Offline
        S Offline
        sikandarhayat
        wrote on last edited by
        #3

        actually clscollection class contain a list of labels and i want if a perticualr label name equals"name" which is exists in the collection class. how can i extract it. thanks for reply

        sikandar

        M 1 Reply Last reply
        0
        • S sikandarhayat

          actually clscollection class contain a list of labels and i want if a perticualr label name equals"name" which is exists in the collection class. how can i extract it. thanks for reply

          sikandar

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, Got it now! foreach(Label l in clscollection) { if(l.Name == "yourename") { //thats it } } All the best, Martin -- modified at 4:17 Friday 25th August, 2006

          S 1 Reply Last reply
          0
          • M Martin 0

            Hello, Got it now! foreach(Label l in clscollection) { if(l.Name == "yourename") { //thats it } } All the best, Martin -- modified at 4:17 Friday 25th August, 2006

            S Offline
            S Offline
            sikandarhayat
            wrote on last edited by
            #5

            hi no not in that way i mean is there any other way loop mean continous searching i want to save time i do not want to compare just assign of perticular. if possible thanks.

            sikandar

            G 1 Reply Last reply
            0
            • S sikandarhayat

              hi no not in that way i mean is there any other way loop mean continous searching i want to save time i do not want to compare just assign of perticular. if possible thanks.

              sikandar

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              An ArrayList (the inner list of the CollectionBase) has nothing that supports accessing items in any other way. You can use a HashTable and use the name of the label as key, then you can access the labels directly by name.

              --- b { font-weight: normal; }

              1 Reply Last reply
              0
              • S sikandarhayat

                how to get single item in foreach loop ?? here is the sample foreach (clsLabels lbl in m_clsLabelCollection) { lstlblname.Items.Add(lbl.label); lstlblvalue.Items.Add(lbl.Value); lstcustext.Items.Add(lbl.customValue); } i want a perticular lable from the clsLabelCollection(collection class inherited from CollectionBase) class clsLabels constructor added to clsLabelCollectionclass clsLabels(string,string,string)

                sikandar

                C Offline
                C Offline
                Coding C
                wrote on last edited by
                #7

                Hi, you can use lbl.Name ie, if (lbl.Name.Equals("your constant value")) { //your code goes here } property. hope this works. Nitin...

                1 Reply Last reply
                0
                • S sikandarhayat

                  how to get single item in foreach loop ?? here is the sample foreach (clsLabels lbl in m_clsLabelCollection) { lstlblname.Items.Add(lbl.label); lstlblvalue.Items.Add(lbl.Value); lstcustext.Items.Add(lbl.customValue); } i want a perticular lable from the clsLabelCollection(collection class inherited from CollectionBase) class clsLabels constructor added to clsLabelCollectionclass clsLabels(string,string,string)

                  sikandar

                  E Offline
                  E Offline
                  Ennis Ray Lynch Jr
                  wrote on last edited by
                  #8

                  Double up the collection. Often I will use both a list and a hash in 1.1. I use the list for iteration and the hash for indexing. If the data is good enough I will keep it in an Array List sorted and Binary Search it.


                  On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                  1 Reply Last reply
                  0
                  • S sikandarhayat

                    how to get single item in foreach loop ?? here is the sample foreach (clsLabels lbl in m_clsLabelCollection) { lstlblname.Items.Add(lbl.label); lstlblvalue.Items.Add(lbl.Value); lstcustext.Items.Add(lbl.customValue); } i want a perticular lable from the clsLabelCollection(collection class inherited from CollectionBase) class clsLabels constructor added to clsLabelCollectionclass clsLabels(string,string,string)

                    sikandar

                    S Offline
                    S Offline
                    Scott Dorman
                    wrote on last edited by
                    #9

                    Since you mentioned that clsLabelCollection inherits from CollectionBase, it should have a Contains and an IndexOf method. Assuming that both of these are actually implemented in the collection class, you can use them to find a specific clsLabels instance: clsLabels l = null; if (m_clsLabelCollection.Contains("value")) { int i = m_clsLabelCollection.IndexOf("value"); l = m_clsLabelCollection[i]; } You can also iterate over the collection (using foreach) and explicitly test to see if the current item is equal to the one you want. When you find the match, break out of the loop. This would be something like this: clsLabels l = null; foreach(clsLabels lbl in m_clsLabelCollection) { if (lbl.label == "value") { l = lbl; break; } } I'm assuming that lbl.label is a string field and that you are looking for the label "value".

                    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