# A little help with a Control[] problem, please.



## Kreij (Oct 14, 2009)

I'm generating controls dynamically based on values retrieved from a database table.
The Names of the controls gets set by concatonating the field name and the Control Type.
There are multiple controls created using the field name, but their names are always unique.
(eg. MyFieldComboBox, MyFieldCheckBox, MyFieldInputTextBox, MyFieldOutputTextBox etc.)

This works fine.

Later in the program I need to parse through the controls to find the specific control I want so that I can retrieve its property values, based on the field name. 

```
Control[] _Controls = new Control[0]; 

foreach (Control c in this.Controls)
{
    if (c.Name.StartsWith(FieldName)
    {
        Array.Resize<Control>(ref _Controls, _Controls.Length + 1);
        _Controls[_Control.Length - 1] = c;
    }
}
```

This too works fine and fills the Control array with all of the Controls that start with FieldName.

Now I want to find a specific type of control in the array by part of its name, knowing in advance what type the control is ...

```
CheckBox _ChB = (CheckBox)Array.Find<Control>(_Controls,
    delegate(Control c) { return c.Name.EndWith("CheckBox"); });
MessageBox.Show(_ChB.Name);
```

Here's where things get funky. The ArrayFind() method finds the control, the MessageBox get displayed and it show the name of the control, but after I press the "OK" button, the program throw a NullReferenceException (Object not set to an instance of an object) on the MessageBox line. 
The debug information show that the variable _ChB is null.

I thought maybe it was some kind of internal windows threading issue, but why would the message box see the name, display it, and then throw an NRE.

Thoughts? Comments?

Probably something simple that I am overlooking. It's been one of those days so far. lol


----------



## Kreij (Oct 14, 2009)

Okay, nevermind. I fixed it. Duh.

There is a label on the form for each row of controls.
I thought I was naming the labels and I wasn't so I was getting one less control than I expect.
So when I used the Array.Length method in an if statement , it was going to the wrong part of the if statment on the next loop.

So all I had to do was change if(_Controls.Length == 2) to if(_Controls.Length == 1)
Like I said, One of those days


----------



## Nailezs (Oct 14, 2009)

awesome...cause i had no clue what u were talking about rofl


----------



## FordGT90Concept (Oct 14, 2009)

I usually write my own methods for searching an array.  The only Array class member I use is Array.Sort<> with a IComparer.

e.g.

```
private int IndexOfName(string name)
{
  int index = -1;
  for (int i = 0; i < _Array.Length; i++)
  {
    if (name == _Array[i])
      index = i;
  }
  return index;
}
```


----------



## Deleted member 67555 (Oct 14, 2009)

Cool next to me Bugging Ford I'll also be bugging you Kreij....
I'M just getting started with stuff....


----------



## Kreij (Oct 14, 2009)

Bug us anytime, jmc, always happy to try to help.

@Ford. The code you presented has to iterate the entire array, continuing to the end even after it found what it needed. But I am well aware that you know how to prevent that 

For the sake of others ... I will ramble a bit 

One way ... 

```
private int IndexOfName(string name)
{
    for (int i = 0; i < Array.length; i++)
        if (name == Array[i]) return i;

    return -1;  // name not found
}
```

Remember that all code paths in a method that returns a value, must return a value of that type. In Visual Studio, the IDE will inform you of that little probelm before you even get to compiling the code.

Also in your code,  make sure that the Array is within a scope that the method can see, pass it into the method or reference it in the method. Again, the VS IDE will inform you that there is a problem if you try to access a variable that is not in scope.

Using the Array.Find<T>(Array, Predicate) method does the iterating for you and stops when the predicate delagate (the method that does the checking) returns a true value. 

It's also kind of nice that you can declare the delegate in-line so you don't have to have a separate method.

If you are using C# 3.0 or greater, you can simplify the code even more by using llamda expressions in the in-line delegate call. (Example by request only. lol)

Supposedly (I did not test it) using the Array.Find<T>  method is faster than using a foreach loop on the array, but I am not sure about a regular for loop.

If anyone wants some indepth information on a particular aspect of C# let me know. I like researching and writing articles.


----------



## Deleted member 67555 (Oct 15, 2009)

Kreij said:


> Bug us anytime, jmc, always happy to try to help.
> 
> @Ford. The code you presented has to iterate the entire array, continuing to the end even after it found what it needed. But I am well aware that you know how to prevent that
> 
> ...


I'll take any info that will be helpful..even if I don't yet know what you are talking about
Here is where i am so far...





Just starting to read..I set it to Visual basic UI...If you think that's not my best route let me know


----------



## Kreij (Oct 15, 2009)

Okay, you got VS installed and it works.
The best thing to do at this point to get help, is to start your own thread in P&W.
That way you will not hijack this thread, and the title will be something like "JMC need help learning Visual Studio", so that people know what they are jumping into to help.

I don't really care about the thread hijack, but it will be better for you to have a thread where we can focus on your needs in programming and it will all be in one place instead of in my thread, and will not get lost in between posts about other stuff.


----------

