On 5/17/07, dkmd_nielsen <donn / cmscms.com> wrote:
> implements an iterator?  I'm just not getting all the new C#
> terminology and syntax complexities, and I need to learn it.  MSDN
> isn't helping me at all.  The following is quasi psuedo mock up of
> what I'm trying to accomplish.
>
>         public void each(Delegate block)
>         {
>             foreach (FL_Instruction i in this.Instructions)
>             {
>                 block;
>             }
>         }

C# gives you the yield keyword now, so I think you can code this
directly, without a delegate:

public void each()
{
  foreach(FL_instruction in in this.Instructions)
    yield i;
}

Haven't used yield myself yet so YMMV. However, as you might see
below, you don't really need this function unless you want to provide
an external iterator because it doesn't do what you want in C#.

>
>         public static FL_Instruction find(string parm)
>         {
>             FindParm fp = delegate(FL_Instruction i) { if
> (i.Parm.IndexOf(parm) > 0) { return i; } };
>             this.each(fp);
>             return null;
>         }

 In C#, delegates represent function signatures. You are wanting to
pass functions with different signatures (including return type) to
each. That works in Ruby but not C#.  Instead, define a function
exists which takes a Predicate delegate (defined in the System
namespace):

// finds an individual instruction
public static FL_instruction find(string parm)
{
  // "anonymous" delegate, which defines a function that takes an FL_instruction
 // and tests it against the passed in "parm" parameter.
  Predicate<FL_Instruction> findParm = delegate(FL_Instruction i) {
return i.Parm.IndexOf(parm) > 0 };

  FL_instruction [] found = this.findAll(findParm);
  if(found.Length > 0)
    return found[0];
  else
    return null;
}

// Function which takes a test and applies it to all the instructions
// in this class. An array of instructions which passed the test are returned,
// or a zero-length array.
public static FL_instruction findAll(Predicate<FL_instruction> test)
{
   ArrayList found = new ArrayList();
   foreach(FL_instruction i in this.INstructions)
    if(test(i))
     found.Add(i)

  return found.ToArray(); // have to convert to array of FL_instructions
}

Array.Exists (and other similar functions) will do all of this and
more, so take a look at them.

Hope that helps!

Justin