Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
GeneralRe: Accessing datasets on form Pin
phimix22-Sep-03 19:26
phimix22-Sep-03 19:26 
GeneralMultithreaded Apps Pin
Ricardo Moncada22-Sep-03 6:04
Ricardo Moncada22-Sep-03 6:04 
GeneralRe: Multithreaded Apps Pin
scadaguy22-Sep-03 8:11
scadaguy22-Sep-03 8:11 
GeneralRe: Multithreaded Apps Pin
Ricardo Moncada22-Sep-03 10:27
Ricardo Moncada22-Sep-03 10:27 
GeneralFreeze form when it removing and reloading ctls Pin
Chris#22-Sep-03 5:57
Chris#22-Sep-03 5:57 
GeneralRe: Freeze form when it removing and reloading ctls Pin
Julian Bucknall [MSFT]22-Sep-03 7:03
Julian Bucknall [MSFT]22-Sep-03 7:03 
GeneralMS Word programming in C#. Pin
bimpy22-Sep-03 5:25
bimpy22-Sep-03 5:25 
GeneralC# switch case style Pin
Don Kackman22-Sep-03 5:24
Don Kackman22-Sep-03 5:24 
In C# it is not legal (or compilable) to let execution fall thru from one switch case context to another.

I'm working on a piece of code to do a MouseHook in C#. Becuase it has to deal with win 32 API messages, I of course have a big long switch statement or two.

An example is deciding which button spawned a particular message, based on the message ID.

In good-ol' C/C++ you'd do it something like this:
C++
int button = 0;
switch ( wParam )
{
  case WM_LBUTTONDOWN:
  case WM_LBUTTONUP:
  case WM_LBUTTONDBLCLK:
     button = LEFT;
     break;

  case WM_RBUTTONDOWN:
  case WM_RBUTTONUP:
  case WM_RBUTTONDBLCLK:
     button = RIGHT;
     break;
//etc. etc.
}
Well that won't compile in C# and given the nature of a C# switch statement I can see two alternatives.
C#
MouseButton button = MouseButtons.None;

switch ( wParam )
{
  case WM_LBUTTONDOWN:
     button = MouseButtons.Left;
     break;
  case WM_LBUTTONUP:
     button = MouseButtons.Left;
     break;
  case WM_LBUTTONDBLCLK:
     button = MouseButtons.Left;
     break;

  case WM_RBUTTONDOWN:
     button = MouseButtons.Right;
     break;
  case WM_RBUTTONUP:
     button = MouseButtons.Right;
     break;
  case WM_RBUTTONDBLCLK:
     button = MouseButtons.Right;
     break;
//etc. etc.
}

or something like :
C#
MouseButton button = MouseButtons.None;

switch ( wParam )
{
  case WM_LBUTTONDOWN:
     goto case WM_LBUTTONDBLCLK;
  case WM_LBUTTONUP:
     goto case WM_LBUTTONDBLCLK;
  case WM_LBUTTONDBLCLK:
     button = MouseButtons.Left;
     break;

  case WM_RBUTTONDOWN:
     goto case WM_RBUTTONDBLCLK;
  case WM_RBUTTONUP:
     goto case WM_RBUTTONDBLCLK;
  case WM_RBUTTONDBLCLK:
     button = MouseButtons.Right;
     break;
//etc. etc.
}

Realizing that big switch statements are notoriously buggy and difficult to maintain, I'm torn between which flavor is less bad.

We've all, of course, been schooled on the harmfulness of goto, and the poor person who's got to debug that code four years from now is in for headache.

On the other hand, without the goto's, you end up with a lot duplicate code (all the assignment statements in the above exaple), which always raises a red flag for me (especially because of the maintainance issues duplicate code can cause).

Anybody got an opinion on this? I'm leaning towards the non-goto version but am looking for other opinions.

Cheers,

don
GeneralRe: C# switch case style Pin
Julian Bucknall [MSFT]22-Sep-03 7:14
Julian Bucknall [MSFT]22-Sep-03 7:14 
GeneralRe: C# switch case style Pin
Don Kackman22-Sep-03 8:39
Don Kackman22-Sep-03 8:39 
GeneralRe: C# switch case style Pin
Julian Bucknall [MSFT]22-Sep-03 10:05
Julian Bucknall [MSFT]22-Sep-03 10:05 
GeneralRe: C# switch case style Pin
Don Kackman22-Sep-03 10:18
Don Kackman22-Sep-03 10:18 
GeneralSmoothing as in MSVisio2002 Pin
Wizard_0122-Sep-03 3:54
Wizard_0122-Sep-03 3:54 
GeneralIE and .NET question Pin
yoaz22-Sep-03 2:34
yoaz22-Sep-03 2:34 
GeneralVisual Studion.Net 2002 closes by itself any time I try loading a C# project Pin
okoji Cyril22-Sep-03 1:20
okoji Cyril22-Sep-03 1:20 
GeneralRe: Visual Studion.Net 2002 closes by itself any time I try loading a C# project Pin
Braulio Dez22-Sep-03 5:06
Braulio Dez22-Sep-03 5:06 
GeneralRe: Visual Studion.Net 2002 closes by itself any time I try loading a C# project Pin
okoji Cyril23-Sep-03 2:02
okoji Cyril23-Sep-03 2:02 
GeneralBooks in C# Pin
nevhile.net22-Sep-03 1:02
nevhile.net22-Sep-03 1:02 
GeneralRe: Books in C# Pin
Azel Low22-Sep-03 3:12
Azel Low22-Sep-03 3:12 
GeneralRe: Books in C# Pin
Braulio Dez22-Sep-03 5:07
Braulio Dez22-Sep-03 5:07 
GeneralRe: Books in C# Pin
phimix22-Sep-03 6:10
phimix22-Sep-03 6:10 
GeneralDowloading a file from a web-site to client using c# Pin
simonzeng22-Sep-03 0:21
simonzeng22-Sep-03 0:21 
Questionhow to parse a dataset Pin
Birdy21-Sep-03 20:51
Birdy21-Sep-03 20:51 
AnswerRe: how to parse a dataset Pin
phimix22-Sep-03 6:13
phimix22-Sep-03 6:13 
GeneralMulti-line ListView control Pin
Member 46249021-Sep-03 20:10
Member 46249021-Sep-03 20:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.