accessing a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Townsend

    accessing a variable

    I have a class that contains three local variables. I need to write a
    method that updates one of these at a time based on a condition. I would
    like to do this without writing a bunch of redundant code. For a very
    simplified example:

    private string x,y,z;

    private void update(DayOfWee k d, int t)
    {
    switch(d)
    {
    case d.Monday:
    switch (t)
    {
    case 1:
    //update variable "x"
    break;
    case 2:
    //update variable "y"
    break;
    }
    case d.Tuesday:
    switch (t)
    {
    case 1:
    //update variable "x"
    break;
    case 2:
    //update variable "y"
    break;
    }
    // ...and so on for the remaining days of the week
    }
    }

    I want to be able to simply call another method in order to eliminate
    the nested switch block, but am unsure about how to go about this.

    Can anybody help me? Thanks!



    *** Sent via Developersdex http://www.developersdex.com ***
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: accessing a variable

    Phil,

    Why not just pass the variable that you want modified by ref, instead of
    t? Then all you need is one switch statement, and you just update the
    variable appropriately.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Phil Townsend" <phil25840@gmai l.comwrote in message
    news:%23fGhLBRz GHA.4396@TK2MSF TNGP04.phx.gbl. ..
    >I have a class that contains three local variables. I need to write a
    method that updates one of these at a time based on a condition. I would
    like to do this without writing a bunch of redundant code. For a very
    simplified example:
    >
    private string x,y,z;
    >
    private void update(DayOfWee k d, int t)
    {
    switch(d)
    {
    case d.Monday:
    switch (t)
    {
    case 1:
    //update variable "x"
    break;
    case 2:
    //update variable "y"
    break;
    }
    case d.Tuesday:
    switch (t)
    {
    case 1:
    //update variable "x"
    break;
    case 2:
    //update variable "y"
    break;
    }
    // ...and so on for the remaining days of the week
    }
    }
    >
    I want to be able to simply call another method in order to eliminate
    the nested switch block, but am unsure about how to go about this.
    >
    Can anybody help me? Thanks!
    >
    >
    >
    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Phil Townsend

      #3
      Re: accessing a variable


      Here is a better example of what I want to do.

      foreach (TimeEntry t in TimeEntries)
      {
      _Total += t.Duration;
      switch (_Categories[t.CategoryId].CategoryType)
      {
      case 0:
      _DirectTotal += t.Duration;
      AddTime(ref DirectEntries, t);
      switch (t.DateCreated. DayOfWeek)
      {
      case DayOfWeek.Frida y:
      _DirectFridayTo tal += t.Duration;
      break;
      case DayOfWeek.Monda y:
      _DirectMondayTo tal += t.Duration;
      break;
      case DayOfWeek.Satur day:
      _DirectSaturday Total += t.Duration;
      break;

      // and so on for the reaminder of the week

      default: break;
      }
      break;
      case 1:
      _IndirectTotal += t.Duration;
      AddTime(ref IndirectEntries , t);
      switch (t.DateCreated. DayOfWeek)
      {
      case DayOfWeek.Frida y:
      _IndirectFriday Total += t.Duration;
      break;
      case DayOfWeek.Monda y:
      _IndirectMonday Total += t.Duration;
      break;
      case DayOfWeek.Satur day:
      _IndirectSaturd ayTotal += t.Duration;
      break;

      // and so on for the reaminder of the week

      default: break;
      }
      break;
      }
      }

      I am trying to figure out a way to avoid all the nested switch blocks
      and how to figure out which variable to update... thx!


      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Bruce Wood

        #4
        Re: accessing a variable


        Phil Townsend wrote:
        Here is a better example of what I want to do.
        >
        foreach (TimeEntry t in TimeEntries)
        {
        _Total += t.Duration;
        switch (_Categories[t.CategoryId].CategoryType)
        {
        case 0:
        _DirectTotal += t.Duration;
        AddTime(ref DirectEntries, t);
        switch (t.DateCreated. DayOfWeek)
        {
        case DayOfWeek.Frida y:
        _DirectFridayTo tal += t.Duration;
        break;
        case DayOfWeek.Monda y:
        _DirectMondayTo tal += t.Duration;
        break;
        case DayOfWeek.Satur day:
        _DirectSaturday Total += t.Duration;
        break;
        >
        // and so on for the reaminder of the week
        >
        default: break;
        }
        break;
        case 1:
        _IndirectTotal += t.Duration;
        AddTime(ref IndirectEntries , t);
        switch (t.DateCreated. DayOfWeek)
        {
        case DayOfWeek.Frida y:
        _IndirectFriday Total += t.Duration;
        break;
        case DayOfWeek.Monda y:
        _IndirectMonday Total += t.Duration;
        break;
        case DayOfWeek.Satur day:
        _IndirectSaturd ayTotal += t.Duration;
        break;
        >
        // and so on for the reaminder of the week
        >
        default: break;
        }
        break;
        }
        }
        >
        I am trying to figure out a way to avoid all the nested switch blocks
        and how to figure out which variable to update... thx!
        Well, for starters, why have separate fields for _DirectFridayTo tal,
        _DirectMondayTo tal, etc. Why not have:

        private int[] _DirectTotals = new int[7];

        then you can do this:

        this._DirectTot als[t.DateCreated.D ayOfWeek] += t.Duration;

        Comment

        Working...