finding an array index

B

Berryl Hesh

The routines below work fine, but is there a more elegant way to do the
second one? It seems there would at least be a built-in function to return
the ordinal position of an element found.

Thanks for sharing - BH

----------------------------------------
/// <summary>Returns an array of the .Net <see cref="DayOfWeek"/>
enumeration, in order, starting on Sunday.</summary>
public static DayOfWeek[] DaysOfTheWeek() {
DayOfWeek[] result = { DayOfWeek.Sunday, DayOfWeek.Monday,
DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday,
DayOfWeek.Friday, DayOfWeek.Saturday };
return result;
}

/// <summary>Returns an array of the .Net <see cref="DayOfWeek"/>
enumeration, in order, starting on the passed <see
cref="DayOfWeek"/>.</summary>
public static DayOfWeek[] DaysOfTheWeek(DayOfWeek dayOfWeekToStartOn) {
var result = new DayOfWeek[7];
int start = 0;
for (int i = 0; i < 7; i++) {
if(DaysOfTheWeek().Equals(dayOfWeekToStartOn)) {
start = i;
break;
}
}
int count = start;
for (int i = 0; i < 7; i++) {
result = DaysOfTheWeek()[count];
if (count == 6 && start != 0)
count = 0;
else count++;
}
return result;
}
 
D

DabblerNL

start=Array.IndexOf(DaysOfTheWeek(), dayOfWeekToStartOn);

is what you are looking for. Several other methods exist that do this.
Find(predicate) on List, or First(predicate) on IEnumerable<T> give similar
results. For instance:
start = (int)DaysOfTheWeek().First(dayOfWeek => dayOfWeek ==
dayOfWeekToStartOn);

You need .Net 3.5 for that one.
 
B

Berryl Hesh

Exactly what I was looking for - much thanks!

DabblerNL said:
start=Array.IndexOf(DaysOfTheWeek(), dayOfWeekToStartOn);

is what you are looking for. Several other methods exist that do this.
Find(predicate) on List, or First(predicate) on IEnumerable<T> give
similar
results. For instance:
start = (int)DaysOfTheWeek().First(dayOfWeek => dayOfWeek ==
dayOfWeekToStartOn);

You need .Net 3.5 for that one.


Berryl Hesh said:
The routines below work fine, but is there a more elegant way to do the
second one? It seems there would at least be a built-in function to
return
the ordinal position of an element found.

Thanks for sharing - BH

----------------------------------------
/// <summary>Returns an array of the .Net <see cref="DayOfWeek"/>
enumeration, in order, starting on Sunday.</summary>
public static DayOfWeek[] DaysOfTheWeek() {
DayOfWeek[] result = { DayOfWeek.Sunday, DayOfWeek.Monday,
DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday,
DayOfWeek.Friday, DayOfWeek.Saturday };
return result;
}

/// <summary>Returns an array of the .Net <see cref="DayOfWeek"/>
enumeration, in order, starting on the passed <see
cref="DayOfWeek"/>.</summary>
public static DayOfWeek[] DaysOfTheWeek(DayOfWeek dayOfWeekToStartOn) {
var result = new DayOfWeek[7];
int start = 0;
for (int i = 0; i < 7; i++) {
if(DaysOfTheWeek().Equals(dayOfWeekToStartOn)) {
start = i;
break;
}
}
int count = start;
for (int i = 0; i < 7; i++) {
result = DaysOfTheWeek()[count];
if (count == 6 && start != 0)
count = 0;
else count++;
}
return result;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top