0

Currently struggling with a issue. I have 30 objects in an array. data[], in each object there is 24 hours. "Hour00", "Hour01" and so on. I want to iterate through these hours. if(data[i].Hour00 > something) But it seems like I cant increment this in an easy way. I tried with the code below but it doesn't allow me to use the string "b" to define which of the hours I want to extract.

for(int k = 0; k < 24; k++)
{
    string b = "Hour00";
    if(data[i].b > 20){
        *Something happens*

}

also tried:

for(int k = 0; k < 24; k++)
{
    if(data[i].Hour(k) > 20){
        *Something happens*

}
10
  • 2
    Can you edit your question to show us the definition of data? What is it an array of? Commented Jul 14, 2016 at 10:25
  • I'm not sure I fully understand but sounds like you'd be better off having a dictionary structure where the key is Hour00, Hour01... and the value is an int. Then you can loop through that much more easily than iterating through all properties and dynamically adding the variable name. Commented Jul 14, 2016 at 10:27
  • 1
    Dayum... this is some really bad understanding of how C# works (and most languages)... but I suggest you look into "C# Reflection" Commented Jul 14, 2016 at 10:27
  • 1
    Why don't you use an int, TimeSpan or DateTime property instead of this string mess? Commented Jul 14, 2016 at 10:29
  • @RiceNor : is "Hour00", "Hour01" and so on is limited to 24? Commented Jul 14, 2016 at 10:29

2 Answers 2

1

Consider a sample class Item

public class Item
{
    public int Hour0 { get;set;}
    public int Hour1 { get;set;}
    public int Hour2 { get;set;}
}

Using reflection you could iterate over HourXX props as:

var item = new Item { Hour0 = 10, Hour1 = 15, Hour2 = 20 };

for (int i = 0; i <= 2; i++)
{
    var hourPropValue = typeof(Item).GetProperty("Hour" + i).GetValue(item);
    // Conditional code here
}
Sign up to request clarification or add additional context in comments.

4 Comments

Helped alot but it wont let me get out the specific data. The Array is like this: Day 1: { Data[0].Hour00 Data[0].Hour01 Data[0].Hour02 etc. The next day: Data[1].Hour00 Data[1].Hour01 Data[1].Hour02 Data[1].Hour03 } and so on. The iterations of days is easy to handle but the hourXX aint.
What is shown above is a iteration for hours. You need outer loop for days. That's it. What is difficult to imagine is why you have such design. There are 30 days 24 hours each. Why would each hour contain a different value that you need if conditions. Not sure what code you are writing for! if condition would make sense if you had only one property that represented hour value.
Thanks @Nikhil figured it out. And thanks for being patient with my formulation of the question. New to this.
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
0

Based upon your comment, now as far as I understand. Assuming that the properties Hour00..Hour23 holding integer values:

int day = DateTime.Today.Day;
for (int i=0; i<24; i++){
  int value = data[day].GetType().GetProperty(string.Format("Hour{0:00}",i)).GetValue(data[day], null);
}

4 Comments

" 30 objects in an array. data[], in each object there is 24 hours. "Hour00", "Hour01" and so on" i think the loop will be on array which has class/struct with HourXX
true, I need to loop through Hour01 Hour02 and so on. if we are on the second this month it would be data[2].HourXX
Added above @RiceNor
Error: (Cannot implicitly convert type "object" to "int" an explicit conversion exists. are you missing a cast?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.