3

I have a need to build some search criteria using the inputs user entered.

string searchText1 = "";
string searchText2 = "";
string searchText3 = "";

searchText1 = scCB1.Text + op1.Text + cr1.Text;
searchText2 = scCB2.Text + op2.Text + cr2.Text;
searchText3 = scCB3.Text + op3.Text + cr3.Text;

where scCB1, op1, cr1, ... are the combobox and text fields on GUI.

Is there a way I could use a loop to replace the hard coded statements (they look awkward)?

For (i = 0; i < 3; i++)
{  
   //    searchText(i) = scCB(i).Text + op(i).Text +cr(i).Text)
}

As a rookie in programming, I understand if I display and build only one set of fields on GUI, then I'll be able to loop in the code. But I am curious how i could address the name of the objects dynamically. Thanks in advance for all your help!

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • 1
    `scCB(i)` have you considered using arrays? The syntax is essentially what you want: `scCBArray[i]` – Flater Aug 06 '18 at 07:33
  • You´re on the right track already, just use an array or a list instead of three different variables. – MakePeaceGreatAgain Aug 06 '18 at 07:36
  • 1
    I'm not quite sure why this is being downvoted. The question is clear, well-formed and shows a reasonable usage and expectation. While the solution is fairly simple, this is a question that most developers will have asked themselves at some point and thus is a relevant question for posterity's sake. – Flater Aug 06 '18 at 07:41

2 Answers2

1

Assuming that the controls are on a GroubBox named options:

string[] searchText = new string[3];
for(int i = 1; i<=3; i++)
   {
        searchText[i - 1] = options.Control["scCB" + i.Tostring()] +
                        options.Control["ops" + i.Tostring()] +
                        options.Control["cr" + i.Tostring()];
   }
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
1

I would suggest to make an array containing your text boxes and other elements, this way your can easily reference them inside a loop:

TextBox[] boxes = {scCB1, scCB2, scCB3}; // do same for op1..3 and cr1..3
for (int i = 0; i < 3; i++)
{     
  string searchText = boxes[i].Text + ops[i].Text + cr[i].Text;
}
Matěj Štágl
  • 870
  • 1
  • 9
  • 27
  • `searchText`s will have to be in a collection as well. And please explain why to use array and not `List`? – Guy Aug 06 '18 at 07:29
  • @Guy, I am suggesting to use an array instead of a list as I assume the count of textboxes to be determined and not to be changed on runtime. If you would add textboxes somewhere in the process then sure, use list or arrayList. – Matěj Štágl Aug 06 '18 at 07:30
  • 1
    @Guy: Most of the argument between array/list has evaporated since they share most of their features by both of them implementing `IEnumerable`. It's easy to jump from one to the other, neither option locks you in. However, as OP wants an indexed approach, arrays are the go-to solution. Note that you can index a list (`myList[i]`), but it's more idiomatic to refer to an array when focusing on indexed element retrieval. – Flater Aug 06 '18 at 07:35
  • To the guy downvoting my answer: please explain the reason. – Matěj Štágl Aug 06 '18 at 07:39
  • 2
    Nobody down-votes your answer. – MakePeaceGreatAgain Aug 06 '18 at 07:40