fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: 'Do Case' - good example to share ???

'Do Case' - good example to share ??? 1 year 2 months ago #1

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi Karl, Wolfgang, all,

Does anyone have a good example (or two) of a CASE statement in X# ?

I don't seem to be able to put my hands on any here in Newport. I have SWITCH okay but not 'Do Case'.

Please attach the code so I can copy and run it before including it in the 'ClickStartXSharp' eNotes.

TIA,
Phil.
Wales, UK.

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #2

Phil,

SWITCH is checking against constanst, DO CASE checks anything.
Local test as STRING
Local age as int
local Name as STRING
... // something stored in test

SWITCH Test
CASE "a" // "a" is a constant
...
CASE "b"
...
OTHERWISE // I have always OTHERWISE, so it's clear there is a hidden path
   NOP    // In this example nothing to do
END SWITCH

DO CASE
CASE age < 50
...
CASE age > 60
...
CASE Name == "Phil"
...
OTHERWISE
   NOP
ENDCASE

I've read that C# will now allow other than const in SWITCH cases. Don't know if X# follow this.

HTH
Frank

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #3

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Thanks a lot Frank,

I will look into this and change my eNotes accordingly.

I will use your sample in my notes if that is OK.

Cheers,
Phil.

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #4

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Frank,

Roberts notes would suggest so - as he calls the SWITCH statement a 'replacement for the DO CASE statement.

Also my own code handles an Enum as seen below :-



Cheers,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #5

Phil,
not sure if i grokked all, but IMHO the crucial point is:
IF , and ONLY IF, your comparing statement tests for EQUAL, AND you are only interested in ONE (the first possible) TRUE, the choice is easy, switch is more efficient and replaces case completely.

In any other case (pun intended), to use switch you have to be absolutely sure that NO other, further, condition might "hit", - if not, you need case.

Sorry, no good example to share, but the above should do ;)


Karl

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #6

Karl,

did you mean two CASE conditions with two different case blocks? Or two differetn CASE conditions with the same case block?

Thas last one comes with (from Phil's example):

SWITCH MaskOption
CASE MaskOption.None
myRXO := RegExOptions.None
CASE MaskOption.IgnoreCase
CASE MaskOption.IgnorePatternWhiteSpace
myRXO := RegexOptions.IgnoreCase // Sorry, it does not make sense, but ... you know
// This case block will be executed for both CASE's
OTHERWISE
myRXO := RegExOptions.None
END SWITCH

If this is not meant by you please give us an example.

I may check it but SWITCH and DO CASE do internally BREAK after the first hit, so no other, additional condition comes into play. Please tell me if I'm wrong. And I'm pretty sur in case of SWITCH there is always only one possible hit. Comparing a value against constants be exact match makes it unambiguous.

Frank

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #7

Frank,
a) Switch only checks one operator, "=="
b) i misread the branching descripton for Do Case, so yes, there is at max one hit per Case/EndCase as is with Switch
c) i heartily dislike the possibility to "stack" cases within Switch without written execution statements - i'm sure i'll never remember, if i only forgot to add them or not.

Karl

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #8

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1406
  • Karma: 6
Hi Phil,

the "switch" statement has only a limited utility for me, as the expressions have to be static ones. This does NOT works:
do switch nValue1
case nValue2
  nop
otherwise
  nop
end switch

whereas the "do case" works:
do case 
case nValue2 == nValue1
  nop
otherwise
  nop
endcase

A sample for a "do case" could be:
do case 
case nValue2 == nValue1 .and. nValue3 == 0
  nop
case nValue2 > nValue1 .and. nValue3 == 0
  nop
case nValue2 < nValue1 .and. nValue3 == 0
  nop
case nValue2 > nValue1
  nop
case nValue2 < nValue1
  nop
otherwise
  nop
endcase

My code is full of such constructs, and I try to use the "do switch" where possible.
This is a piece of code from my Door Configurator:

It should explain why the use of a "do case" is much more flexible than a "do switch".
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it
Attachments:

Please Log in or Create an account to join the conversation.

Last edit: by wriedmann.

'Do Case' - good example to share ??? 1 year 2 months ago #9

Hi,

@Karl:
Unit tests are your friend :-). Once you are familiar with you know the outstanding value.

@Wolfgang:
Your example shows what is named 'feature envy'. Put the boolean logic to where it belongs, the class oVariante, as some kind of 'super property' and return an enum. Then you can a) unit test your class b) use SWITCH. Of course it does not affect execution time but makes the code much cleaner.

@All:
I changed more than 90% of my DO CASES to SWITCH and found a bunch of bugs because in the case of SWITCH the compiler is able to check of double CASE's. With DO CASE this compiles ok:
DO CASE
CASE Value == "a"
   DoThis()
CASE VALUE == "b"
   DoThat()
CASE VALUE == "a" // the same as the first CASE
   DoSomethingOther() // different CASE block.
OTHERWISE
   NOP
ENDCASE
This gives a compiler error:
SWITCH Value
CASE "a"
   DoThis()
CASE "b"
   DoThat()
CASE "a" // This is not allowed by the compiler
   DoSomethingOther() // different CASE block.
OTHERWISE
   NOP
END SWITCH

When eliminating the 'feature envy' as described we can change almost all DO CASE's to SWITCH. That gives us more readable and better testable code, means less bugs.

Frank

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #10

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1406
  • Karma: 6
Hi Frank,

I was sure you would ask me to change my code.

Unfortunately this code needs to be there as it has nothing to do with the AngebotVariante itself, but only how to edit it. Therefore it makes more sense (to me) to leave it in the ViewModel code.

For sure, my programming style is not the best one, and there are much better programmers than me here. And I admit than I have to change many things to make them better, but this piece of code is not on this list.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #11

Wolfgang,

I hope you don't take it personal. We all have good and not so good code. But here we want to help each other. In this sense I take the opportunity to show how code can be made better. And: Phil asked for 'good example to share'.

I'm very happy about that you show us a bunch of code. This has great value for the community. Thanks a lot!

I wish you, your family and all other people here and their families a merry christmas and a happy new year.

Frank

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #12

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1406
  • Karma: 6
Hi Frank,

unfortunately I'm not a very good programmer, I have only a bit of experience.

But "good code" or "better code" IMHO is not an absolute thing, but a personal opinion (like "what is a beautiful woman/man"). And it depends very much on the context, unfortunately.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #13

Hi Wolfgang,

I would not say that. You have a complex and feature rich application running almost bug free. You must be a good programmer!

If you or others like I will show in more detail later how I would refactor your code and why. BTW: My code looks he same after the first run. And if I'm in time pressure I let it like it is, because it works. But later, especially when I want to add features I do refactoring first. It simply makes live easier.

Fore those who ask me why I do not show my own code before and after: I'm not Phil who is good in documenting all what he is doing. I do not have a source control system. I do not work to present the formal transformation. Saying that: I do not hve a before/after example becaus 'before' is deleted or hard to recover. I know this is not how professionals should work :(.

Frank

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #14

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Thanks Karl,

Will work on it.

I have to admit that "Do Case" and "If Else Endif" are a part of program design that leaves me feeling quite uncomfortable.

Its probably the area where most logical errors are entered by the developer.


Speak soon,
phil.

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #15

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Thank you guys, one and all,

I knew before I asked the Forum community that this topic of Do Case and Switch was a potentially difficult one - to get right in my eNotes and to also show all the possibilities etc..

Maybe we need to work together some more - to get stuff 100% correct.

There is no point in me just providing simple examples for either/both 'Do Case' and 'switch'.

Possibly the problems that we all have is that in this current/modern world we write our 'Do Case' code just as we did MANY years back, and maybe it is time for us to get a new approach to the multiple switch code situation !?

Thanks for your help - keep it coming please ;-0)

Have a joyful Christmas and a Happy New Year :kiss::kiss::kiss:

Cheers,
Phil.
Wales, UK

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #16

Frank,
i still miss a sample how to code any comparison other than equality with Switch...

Karl

Please Log in or Create an account to join the conversation.

'Do Case' - good example to share ??? 1 year 2 months ago #17

Hi Karl,

the answer is simple: not possible yet!

Until C# 6 switch only works by comparison with bool, int, const, enum, char and a literal string.

C# 7 introduces all kinds of comparisons.

As I know and would like to leave as that in the future X# reflects C# 6 behaviour.
For the C# 7 enhancements we have DO CASE already.

Frank

Please Log in or Create an account to join the conversation.

Last edit: by Frank Maraite. Reason: typo, misread who asked
  • Page:
  • 1