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