Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

accessing a variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Phil Townsend - 31 Aug 2006 16:15 GMT
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(DayOfWeek 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!
Nicholas Paldino [.NET/C# MVP] - 31 Aug 2006 16:40 GMT
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.

Signature

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

>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
[quoted text clipped - 37 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Phil Townsend - 31 Aug 2006 18:43 GMT
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.Friday:
                               _DirectFridayTotal += t.Duration;
                               break;
                           case DayOfWeek.Monday:
                               _DirectMondayTotal += t.Duration;
                               break;
                           case DayOfWeek.Saturday:
                               _DirectSaturdayTotal += 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.Friday:
                               _IndirectFridayTotal += t.Duration;
                               break;
                           case DayOfWeek.Monday:
                               _IndirectMondayTotal += t.Duration;
                               break;
                           case DayOfWeek.Saturday:
                               _IndirectSaturdayTotal += 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!
Bruce Wood - 31 Aug 2006 18:51 GMT
> Here is a better example of what I want to do.
>
[quoted text clipped - 48 lines]
> 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 _DirectFridayTotal,
_DirectMondayTotal, etc. Why not have:

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

then you can do this:

this._DirectTotals[t.DateCreated.DayOfWeek] += t.Duration;

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.