-1

I am working with a WPF in C#. I am using the GetNextControl method to store all the child controls in a Control.ControlCollection. I want to loop through the results and fill in only the text boxes. I have thought of two ways to do this, but which would be more efficient:

  1. Search once and store the results in an Control.ControlCollection.
  2. Use a foreach loop to go through the collection and use multiple if/else statements to find the TextBox I am looking for and fill in the box with some text.

Or,

  1. Search and store all the controls in a Control.ControlCollection.
  2. Use the find method of the collection to find a TextBoxwith a certain name and fill in some text in the TextBox.

I think that the first way would be slower because there are more comparisons to make. While the second method uses searching only.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Darren Hoehna
  • 269
  • 2
  • 8
  • 19
  • 2
    Two things - You did not ask a question here, and what is stopping you from trying both and measuring? – asawyer Aug 12 '13 at 19:05
  • How do you think "searching only" is implemented? But you probably don't need to worry -- most likely any solution is going to be good enough because the problem size is tiny. – Jon Aug 12 '13 at 19:05
  • 1
    Part 3 - `OfType()` ? – Sayse Aug 12 '13 at 19:05
  • UI controls have tree structures, so search algorithms are fast enough and if existing one is not fast enough you can implement your own. But results may vary depending on your UI architecture. The only thing we can suggest is: profiling. – Leri Aug 12 '13 at 19:05
  • 3
    Forget all that. Learn MVVM before you ever write a single line of code in WPF. You're doing a lot of unnecessary stuff. – Federico Berasategui Aug 12 '13 at 19:06
  • @HighCore: I second the motion. [*Here's how I dealt with all that*](http://stackoverflow.com/a/14466547/23771), ages ago. The idea is not to deal at all with controls as data objects. Let the "library" handle that. – Mike Dunlavey Aug 12 '13 at 19:11
  • @asawyer Nothing is stopping me from trying both and measuring. I just wanted the insight from other people who have coded longer than I have. That's all. – Darren Hoehna Aug 12 '13 at 19:18
  • @DarrenHoehna if you want insight, I will say: `DO NOT manipulate UI elements in procedural code. Learn MVVM`. – Federico Berasategui Aug 12 '13 at 19:27

1 Answers1

2

Implement the easiest one. Do not worry about optimization until you have metrics to support the need.

If it is not fast enough/efficient enough, then get some good time measurements. Now it is time to consider alternate implementations.

Implement and time each of the alternates, picking the fastest/most efficient one.

Colin D
  • 5,641
  • 1
  • 23
  • 35