Eleanor
Eleanor

Reputation: 569

Select next child in array using c#

I have an array,

 string [] myString = new string["2","1","3","2","1","2"];

And I want when I select one child I will get the next child value, That is to say, when I select the first one"2" , I will get the value of the second"1"...When I select the second one "1",I will get the value of the third one"3"...and so on... I try to use index method, here is my code:

foreach (var index in myString){
int myindex = myString.indexOf(index);
}

Well, it is not the correct method...Any one know, how to do that? Thanks

Upvotes: 3

Views: 13871

Answers (5)

amcdermott
amcdermott

Reputation: 1585

So I'm assuming that you want a function that will take the string[] and an int and return the next element (or the first element - if the int value indicates the last element). Something like this will do the trick ... (note I've just thrown the code in here without attempting to compile or testing so may need a tweak or two).

public string GetNextElement(string[] strArray, int index)
{
    if ((index > strArray.Length -1) || (index < 0))
       throw new Exception("Invalid index");

    else if (index == strArray.Length -1)
       index = 0;

    else
       index++;

    return strArray[index];
}

for a list you would have something like (assuming you always have a list of string objects - use ToString() otherwise)

    public string GetNextElement(IList<string> list, int index)
    {
        if ((index > list.Count - 1) || (index < 0))
            throw new Exception("Invalid index");

        else if (index == list.Count - 1)
            index = 0;

        else
            index++;

        return list[index];
    }

Upvotes: 6

LocEngineer
LocEngineer

Reputation: 2917

If I get you correctly this should do the trick:

string ChildValue(string[] myString, int which)
{
    if (which < myString.GetUpperBound(0))
        return myString[which + 1];
    else
        return myString[0];
}

You call this function like from wherever, like this (passing your string array and the desired index as arguments:

public void whatever()
{
    string[] myString = {"2", "1", "3", "2", "1", "2"};
    var selectedChildValue = ChildValue(myString, 3);
}

And as return value you will get the value of element 4.

Upvotes: 4

ConnectingKamlesh
ConnectingKamlesh

Reputation: 89

for( int i = 1; i <= myString.Length; i++ )
{
   string s = i == (myString.Length -1) ? myString[0]: myString[i] ;
}

Upvotes: 2

w.b
w.b

Reputation: 11238

This will do if you want to get the first element for the last index:

string[] myString = new string[] { "2", "1", "3", "2", "1", "2" };
for (int i = 0; i < myString.Length; ++i)
{           
    string s = i == myString.Length - 1 ? myString[0] :  myString[i + 1];
    Console.WriteLine(s);
}

Upvotes: 1

Mario The Spoon
Mario The Spoon

Reputation: 4869

for( int i = 0; i < myString.Length - 1; ++i )
{
   string s = myString[i+1];
}

If I understood you correctly

Edit: added the +1 to the index within the loop

Upvotes: 1

Related Questions