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 create Unique Named Objects in a loop

How to create Unique Named Objects in a loop

Scheduled Pinned Locked Moved C#
csharpcssdesignhelptutorial
4 Posts 2 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.
  • M Offline
    M Offline
    MarkB123
    wrote on last edited by
    #1

    Hi, I have the following issue in C# 2, can someone point me in the correct direction please? I need to instantiate a combobox object (one for each column in my DevExpress grid control). At design time, I have no idea how many columns the grid will have so I need to iterate through each in a loop. Each combobox will need to have a different name e.g. col.FieldName + "ComboBox". However, I can't get the syntax correct. Current Code... string ComboBoxName = ""; foreach (GridColumn col in view.VisibleColumns) { ComboBoxName = col.FieldName + "ComboBox"; RepositoryItemComboBox ComboBoxName = new RepositoryItemComboBox(); // Fails on this line as compiler says I'm attempting to redefine the ComboBoxName object // } Any suggestions? Thanks

    L 1 Reply Last reply
    0
    • M MarkB123

      Hi, I have the following issue in C# 2, can someone point me in the correct direction please? I need to instantiate a combobox object (one for each column in my DevExpress grid control). At design time, I have no idea how many columns the grid will have so I need to iterate through each in a loop. Each combobox will need to have a different name e.g. col.FieldName + "ComboBox". However, I can't get the syntax correct. Current Code... string ComboBoxName = ""; foreach (GridColumn col in view.VisibleColumns) { ComboBoxName = col.FieldName + "ComboBox"; RepositoryItemComboBox ComboBoxName = new RepositoryItemComboBox(); // Fails on this line as compiler says I'm attempting to redefine the ComboBoxName object // } Any suggestions? Thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, In your code, you have the same name (ComboBoxName) for a string and for a RepositoryItemComboBox, which is unacceptable. you are confusing two concepts: 1. each Control has a Name property, that is a string that is available at run-time, so you could find a Control "by name" just by iterating over tham all and recognizing the Name. Ir is primarily used by Visual Designer. If you don't use Designer, you may not need to assign a value to Name at all. 2. each Control may (or may not) have a reference stored in a variable, obviously a variable has a name. Controls don't need to have public variables refering to them, as soon as they are added to the Controls property of a Container (say a Form), they become part of that Container. I will assume you are not really interested in the Name property, and want to hold references to a variable number of RepositoryItemComboBox objects. This would work for you then:

      List< RepositoryItemComboBox> myRICBlist=new List< RepositoryItemComboBox>();
      foreach (GridColumn col in view.VisibleColumns) {
      RepositoryItemComboBox comboBox = new RepositoryItemComboBox();
      myRICBlist.Add(comboBox);
      }

      and later on you could iterate all those comboboxes with:

      foreach (RepositoryItemComboBox comboBox in myRICBlist) {
      comboBox.Clear();
      }

      :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, In your code, you have the same name (ComboBoxName) for a string and for a RepositoryItemComboBox, which is unacceptable. you are confusing two concepts: 1. each Control has a Name property, that is a string that is available at run-time, so you could find a Control "by name" just by iterating over tham all and recognizing the Name. Ir is primarily used by Visual Designer. If you don't use Designer, you may not need to assign a value to Name at all. 2. each Control may (or may not) have a reference stored in a variable, obviously a variable has a name. Controls don't need to have public variables refering to them, as soon as they are added to the Controls property of a Container (say a Form), they become part of that Container. I will assume you are not really interested in the Name property, and want to hold references to a variable number of RepositoryItemComboBox objects. This would work for you then:

        List< RepositoryItemComboBox> myRICBlist=new List< RepositoryItemComboBox>();
        foreach (GridColumn col in view.VisibleColumns) {
        RepositoryItemComboBox comboBox = new RepositoryItemComboBox();
        myRICBlist.Add(comboBox);
        }

        and later on you could iterate all those comboboxes with:

        foreach (RepositoryItemComboBox comboBox in myRICBlist) {
        comboBox.Clear();
        }

        :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


        M Offline
        M Offline
        MarkB123
        wrote on last edited by
        #3

        Perfect, Thanks Luc. :)

        L 1 Reply Last reply
        0
        • M MarkB123

          Perfect, Thanks Luc. :)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          you're welcome. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


          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