473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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(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 ***
Aug 31 '06 #1
3 1340
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]
- mv*@spam.guard.caspershouse.com

"Phil Townsend" <ph*******@gmail.comwrote in message
news:%2****************@TK2MSFTNGP04.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(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 ***

Aug 31 '06 #2

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 ***
Aug 31 '06 #3

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;

Aug 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ms_chika | last post by:
Hi to all, I have this problem in xsl wherein i want to access a variable in javascript and use it my xsl. How would i access or use a javscript variable in my xsl file? Please help. ...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
5
by: Siva | last post by:
Hello I have a dropdownlist inside the gridview as a template column defined as follows: <asp:TemplateField HeaderText="Choose Location"> <ItemTemplate> <asp:DropDownList ID="ddlChooseLoc"...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
2
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class...
6
by: psbasha | last post by:
The following example shows the storing of the data and accessing the data from dictionary variable.The accessing of dictionary variable data is not a orderas what we are storing. Is my...
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.