Foreach alternative

Mar 18, 2011 at 6:45 PM

Ok I have found a way around using foreach. It requires more code but it works on any array.

 

Add this to your source,

static int i = 0;

public static object _objectArray(object[] objectARRAY)
        {

            while (i <= objectARRAY.Length)
            {
                object Toreturn = objectARRAY[i];
                i++;
                return Toreturn;
            }
            i = 0;
            return "ErroR";
        }
That will return each value in the array. It returns each value then terminates by sending the error string.

To access returned info adopt the below code.

bool run = true;
            while (run == true)
            {
                object y = Foreachalt._objectArray(//Insert array);
                if (y.ToString().ToLower() == "error")
                {
                    run = false;//stops it from returning anymore values.
                }
                else
                {
                    //Y is now the return value.
                   //for example if it was a string this would work,
                   // MessageBox.Show(y.ToString());
                }
            }


Comment any problems and i will be happy to help.


 

Coordinator
Mar 20, 2011 at 12:48 PM
Edited Mar 20, 2011 at 12:49 PM

I think you are over complicating things.

People coded a long time before the foreach syntactic sugar.

foreach (var x in array)

becomes

for (int i=0; i<array.length; i++)
{
  var x= array[i];
}

and 

 
foreach (var x in enumerable)

becomes

for (var e = enumerable.GetEnumerator(); e.MoveNext(); )
{
var x = e.Current;
}
Coordinator
Mar 20, 2011 at 12:50 PM
That latter variant might not always work, as most of the time you'll get interfaces....


On Sun, Mar 20, 2011 at 12:48 PM, smremde <notifications@codeplex.com> wrote:

From: smremde

I think you are over complicating things.

People coded a long time before the foreach syntactic sugar.

foreach (var x in array)

becomes

for (int i=0; i<array.length; i++)
{
  var x= array[i];
}

and

foreach (var x in enumerable)

becomes

for (var e = enumerable.GetEnumerator(); e.MoveNext(); )
{
var x = e.Current;
}

Read the full discussion online.

To add a post to this discussion, reply to this email (Cosmos@discussions.codeplex.com)

To start a new discussion for this project, email Cosmos@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Mar 20, 2011 at 12:52 PM
Edited Mar 20, 2011 at 12:52 PM

Fine, but my point was: You don't need to write support classes to iterate over an array :)