C # Properties - Required Helper Properties

I was tasked with parsing XML data and json data in an application. I am trying to create a property class that covers all the data that I am collecting.

Here is my question / question

I created a class with variables for weather data, temp, wind, uv index, etc. I also created days. I can access days individually, but not in general. For example.

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

      

I want to do this.

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

      

Etc. I'm new to C # and I'm not even sure what to do with Google. I rack my brains week after week and come with limited success. I am open to other data storage suggestions.

+3


source to share


3 answers


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

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

      

Then the class Day

has a property TempHiF

, etc .:



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

      

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

+6


source


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



class Day {
// Define day-bound properties here
}

      



+1


source


You just need to nest your classes so that your weekday class has a Monday class, a default class, etc., then give each of your daytime classes a TempHi property or whatever additional properties you want, then just refer to these

class WDay{

  public Monday mon = new Monday();
  public Tuesday tue = new Tuesday();
  public Wednesday wed = new Wednesday();

}

class Monday
{
  private string _TempHi;
  public TempHi
  {
    get {
     return _TempHi;
    }
    set {
     _TempHi = value;
    }
  }
}

class main
{
  WDay WeekDay = new WDay();

  WeekDay.mon.TempHi = "65F";
}

      

+1


source







All Articles