Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Properties - Need Sub Properties

I have been tasked with parsing out xml and json data into an application. I am trying to create a properties class that encompasses all the data I will be collecting.

Here is my issue/question

I have created a class with the variables for the weather data, temp, wind, uv index, etc. I have created the days as well. I can access the days individually but not as a whole. For example.

Monday m = new Monday();
m.TempHiF = "65";

What I want to do is this.

WDay d = new WDay();
d.Monday.TempHiF = "65"
d.Tuesday.TempHiF = "67";

And so on. I am pretty new to C# and I am not even sure what to google. I have been racking my brain for a week and come up with limited success. I am open to other suggestions on storing the data as well.

like image 408
JD Roberson Avatar asked May 15 '26 00:05

JD Roberson


2 Answers

All you have to do is make WDay have properties for all the days:

public class WDay
{
    public Day Monday {get;set;}
    ...

Then have the Day class have a TempHiF property, and so on:

public class Day
{
    public string TempHif {get;set;}
    ...
}

Make sure WDay's constructor initializes all its Day properties with new instances to avoid null reference exceptions.

like image 133
StriplingWarrior Avatar answered May 17 '26 13:05

StriplingWarrior


class Week {
  public Day Sunday{get;set;}
  public Day Monday{get;set;}
  // etc...
}



class Day {
// Define day-bound properties here
}
like image 37
Sam Axe Avatar answered May 17 '26 14:05

Sam Axe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!