- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
WTF! It's called "Smörgåsbord".
Don't you speak swedish. :oP
Admin
In what way exactly? If LoadPageData has some way of telling what the page state is, what do you need select statement for?
Cool, my captcha was zork.. ZOOOOOOOOOOOOORK!!!!!!
Admin
It looks like that code was written by a programmer who gets paid for the number of lines of code (s)he produces...
Admin
I think you meant:
int s = 0;
switch(s) {
0: //Load Page Data
LoadPageData();
break;
1: // ????
dontBotherBreaking();
2: // ????
Profit!
}
dZ.
Admin
What do you mean?? I can see the forest. Of course I can see the forest!! Its just all this damn trees that keep getting in the way!
dZ.
Admin
Yaaaaaay! I was almost done with the first page and wondered when the "repost" jokes were going to kick in.
dZ.
Admin
A little bit of copy+pasting trouble there, uh? :)
dZ.
Admin
This reminds me of an Asterix comic, where the vikings' dialogs were shown normally but with umlauts on their 'u's and slashes through their 'o's to show that they were speaking a different language.
dZ.
Admin
Firstly you're wrong - C# does it the C way.
Als, as was pointed out by someone else, VB is the most elegant for this sort of thing by far.
Select Case PlantHeightInMM
Case Is < 1
Msgbox "WTF?"
Case 60, 71, 80
Msgbox "That is the same size as one of my plants. OMG WOW!"
Case 1 to 50, Is > 100
Msgbox "That's not a valid plant size."
Case 51 to 100
Msgbox "Just tall enough"
Case
End Select
Of course, Selects can be abused, even in VB.
Admin
Never touched c# ... bad karma.
I think what Anonymous meant was that c# does not allow fall-through. The do allow multiple labels though.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfTheSwitchStatement.asp
So unless you want to beautify your code with nice-looking goto - no fall-through for you <:o)
poor duff
Admin
Heh, that won't compile.
Admin
I totally read this method as <FONT face="Courier New">GetPreciousForm()</FONT>. I was wondering what the class declaration would be like:
<FONT face="Courier New"><FONT style="BACKGROUND-COLOR: #ffffff" color=#0000ff>using</FONT> System.Windows.Forms;
</FONT><FONT face="Courier New">
<FONT color=#0000ff>public class</FONT> MyPreciousssssssssssssssssssss : Form
{
}</FONT>
Admin
Oh, and you can't color-code any of your posts... otherwise it looks like [6]
(in time: only a few of the available emoticons actually render as one... a lot don't: [pi] [au] [um] [mo] [C] [{] [}] [W] [B] etc...
Admin
If at first you don't succeed, give up. - Homer Simpson.
Admin
Reminds me of production code in a live app:
private string AppID
{
get { return Request.QueryString["AppID"] != string.Empty ? Request.QueryString["AppID"] : string.Empty; }
}
Some programmers are good at writing tautology. Others are good at dribbling.
Admin
C# allows fall through switch statements IF there are no statements in the case block
For example:
switch( foo )
{
case 1: case 2: case 3: bar(); break; // this works fine because of the empty case labels
}
switch( foo )
{
case 1: bar(); //compile error b/c there is no break
case 2: bar();
}
Admin
Ha! Like VB is "most elegant" at anything (not that I think it doesn't have it's uses, but elegance simply isn't how I'd characterize it.
Compare perl (can use integers, strings, slices, arrays, regex patterns, hashes, and subroutines in its switch statement):
Admin
For the love of <insert your deity here>, I hope you're not referring to the "...", or the fact that I didn't include the import statements. Of course it won't compile!
Admin
Probably referring to the fact that you have a throw saxe; in the final catch, which of course would be out of scope...
Admin
Ah, fair enough. What do you want for an example?!?! :^)
Admin
I think lucio was referring to the throw saxe in the catch nfe section. Though getting back to your original example, how else can you log the exceptions and then rethrow and retain the throws signature of the method? As long as you have checked exceptions, you will be stuck with having to write crap like this.
Admin
What about if the range is not inclusive as in >=51 to <100?
I prefer xBASE. The condition can be arbitrary:
<font size="2">do case
case PlantHeightInMM<1
? "WTF?"
case inlist(PlantHeightInMM,60,71,80)
? "That is the same size as one of my plants. OMG WOW!"
case between(PlantHeightInMM,1,50) or PlantHeightInMM>100
? "That's not a valid plant size."
case between(PlantHeightinMM,51,100)
? "Just tall enough"
endcase
</font>
Repeating the variable name is not ideal, but it is very handy when the condition involves more than one variable. I have some code where there are three subexpressions to iterate through. It is much more readable without nested cases. (YM and preferences MV.)
Sincerely,
Gene Wirchenko
Admin
Shouldn't you be doing something like this:
Gollum.GetPreciousFrom(filthyHobbits)
or even
this.GetPreciousFrom(filthyHobbits)
assuming this is the object in which state is held
It is counter intuitive to have the filthyHobbits perform GetPreciousFrom() when the desired goal is to have the precious leave the possession of the filthyHobbits.
"Return with your shield, or on it"
Admin
The answer to that question depends a lot on where in the application the Exception was thrown. If it's thrown somewhere within the business layer, once it propagates up to the public interface of the business layer you, at a minimum, wrap it in a generic or application specific Exception type.
throw new Exception("Some error", saxe)
or
throw new BusinessLayerException("Some error", saxe)
or
throw new RemoteException("Some error", saxe)
Admin
That's possible in VB too (.net and the oldies):
Select Case True
Case PlantHeightInMM<1
...
Case PlantHeightInMM>100
...
End Select
(Don't know about C#)
Admin
But what if I want to log and rethrow WITHOUT wrapping? After all, the catch block may have access to critical information that I wish to log (for example, the name of a file that was not found, the SQL of a query which failed, or the UID of a row which failed validation) without being the ideal place to wrap/handle.
Admin
SelectedValue returns the "Selected Value" not the "Text". The select value could be an ID and not the Text displayed in the dropdown list. Jakeypoo's code sucks but your suggestion is a bug and that is worse.
A better way to get the text, that doesn't introduce a bug would be the following:
lblNavigation.Text = ddlOperationList.SelectedItem.Text
Also, writing lblNavigation.Text = ddlOperation.SelectValue.ToString(); would be foolish since SelectedValue clearly returns a string value.<FONT size=1>
</FONT>Admin
If you want to log and throw without wrapping got right ahead. But, in addition to throwing an exception, you may be throwing out modularity. The general principal is that the caller of a method shouldn't have specific knowledge of how the method does it's work. If you see a method that throws an IOException, a SAXException and a NumberFormatException, you have a very good idea of what it's doing. I would give you 1000 to 1 odds, after seeing only the signature, that that method is opening an XML file, parsing it and trying to convert some of its data to numbers. As I said, the granularity of exceptions thrown depends a lot on the context.
Admin
At least the first one is commented.
Admin
For some reason, i have a feeling that the writer of the first code would do another "switch" check in "
Admin
There's a switch statement in Perl?? Where the fsck have I been??!
dZ.
Admin
Apparently as of 5.8 there is. It's pretty awkward though. I think there are some PMs out there too.
Admin
Well my point wasn't to say "VB is the best!", just to show that C has probably the least useful case system I've ever seen. VB just happens to be the one I'm most used to. xBase's one looks pretty neat.
Admin
I was pointing out alternatives, too.
C's switch statement is braindead. It is very good for generating very efficient object code. I can see how it can be compiled easily into a jump table. The problem is that I rarely code in such a way that C's switch statement is of use to me. The only time I do that is when writing a menu (and the switch does not save much there) or some sort of interpreter.
C gives too much to the corner cases and causes trouble for the more usual cases. (Omit a break statement to see what I mean.)
Sincerely,
Gene Wirchenko
Admin
That makes sense, but only if the code is an If...Else If...Else... How does the If...Else... of the original post handle the situation that you point out?
Admin
no it doesn't. for that to for this to make sense you'd need to remove the breaks...
Admin
Obviously, this programmer is paid by the line.