PC Review


Reply
Thread Tools Rate Thread

accessing a variable

 
 
Phil Townsend
Guest
Posts: n/a
 
      31st Aug 2006
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!



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      31st Aug 2006
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]
- (E-Mail Removed)

"Phil Townsend" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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!
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
Phil Townsend
Guest
Posts: n/a
 
      31st Aug 2006

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!


*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      31st Aug 2006

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.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!


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;

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing fields via variable John Microsoft Access 5 24th Mar 2008 12:44 PM
Accessing fields via variable John Microsoft Access Form Coding 5 24th Mar 2008 12:44 PM
Accessing Masterpage Variable Al Microsoft ASP .NET 3 2nd Jul 2007 10:31 AM
accessing a public variable DC Gringo Microsoft ASP .NET 2 30th Dec 2004 01:27 PM
Accessing the name of a variable via reflection Ken Durden Microsoft C# .NET 3 22nd Dec 2003 08:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:49 PM.