|
I don't understand. Your example of stacking up case labels does compile in C#. Here's a small example:
Random r = new Random();
switch (r.Next(10)) {
case 1:
Console.WriteLine("It's one");
break;
case 2:
case 3:
case 5:
case 7:
Console.WriteLine("It's prime");
break;
default:
Console.WriteLine("It's composite");
break;
}
Cheers, Julian
Program Manager, C#
This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
|
|
I was getting an error when I was setting up a switch block, and on further examination it looks like it was because I failed to include a break in my default case.
Without the break in the last case statement you get something like: "error CS0163: Control cannot fall through from one case label ('case 2:') to another"
However, when I first encountered the error I found this in the C# MSDN reference on the switch statement:
"Unlike the C++ switch statement, C# does not support an explicit fall through from one case label to another. If you want, you can use goto a switch-case, or goto default."
Now I'm confused, because that apparently doesn't mean what I thought it meant.
The way I read the statement from MSDN, plus if you read the help on error number CS0163 (see reference below), it seems to support my original post.
It seems like either the C# help is in error, or the compiler is in error (or I'm just plain missing something).
don
References
Error CS0163:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cscomp/html/vcerrCompilerErrorSC0163.htm
Switch statement help:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/csref/html/vclrfTheSwitchStatement.htm
|
|
|
|
|
Ah ha! You're reading the "simplified" pages and I'm not surprised that you found ambiguity. Personally I tend to go to the authoritative source, the C# Language Reference. Here you see that
a switch statement consists of "switch" followed by an expression in parentheses followed by a switch block
a switch block consists of 0 or more switch sections, the whole wrapped in braces
a switch section consists of one or more switch labels followed by a list of statements
a switch label is either "case" followed by a constant expression followed by colon, or "default" followed by colon.
It is the statement list that cannot "fall through." (In fact, it's stated as "the end point of a statement list cannot be reachable," by which it means that you cannot get to the point just after the last statement in the statement list. In other words, the last statement in the list must be a "jump" type statement: break, goto, throw, etc.)
Hence, <phew>, you can have code that lokos like this:
switch (foo) {
case 1:
case 2:
DoSomething();
break;
...
}
As to the error message, I agree that it could be a little misleading, although it does correctly talk about the last switch label in the (possible) list of labels.
Cheers, Julian
Program Manager, C#
This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
|
|
And the light comes on!
It does makes sense now (and also renders my original post completely moot).
Thanks for clearing up the ambiguity for me Julian!
don
To summarize
legal:
switch case( foo )
{
case 0:
case 1:
DoIt();
break;
} not-legal:
switch ( foo )
{
case 0:
DoOneThing();
case 1:
DoAnotherThing();
break;
} also not legal:
switch ( foo )
{
case 0:
DoOneThing();
break;
default:
DoAnotherThing();
}
|
|
|
|
|
I found that GDI+ has some features for smoothing (SmoothingMode,TextRenderingHint...). MSVisio2002 also uses GDI+ (I suppose). But graphics in Visio and my GDI+ application looks different. It seems that Visio uses another smoothing mode, do you know which ?
|
|
|
|
|
MS used to have a thing called "Browser Helper Object", supporting IE4 (maybe 5). Is there a newer parllel to this? To be specific, I would like to gather statistics on my browser, such as download time of each object in a web page. Are there events raised by IE handling such questions?
at first stage, I don't mind activating the browser programmtically (I gather that the strongest feature of the Browser Helper was that it attached itself to any instance of the browser )
YD
|
|
|
|
|
Hi All,
Recently I loaded a C# project am writing using VS.Net 2002 all of a sudden the development enviroment closes my project by it self. It repeated several time any time I load the project and start coding.
I've even reinstalled VS.Net 2002 in an XP OS, tried to open the same project from another PC (Win2k Pro) having VS.Net 2002 . Visual studio.Net 2002
still close itself after allowing me to work for a while.
I am waiting for your sugestions or advice on what to do. I don't know wheather it is a bug with Visual Studio 2002.
Thanks
(I've posted the same question on Visaul studion IDE board)
|
|
|
|
|
Mmm...
Some tips, XP, XP Service Packs. something wrong with your version...
|
|
|
|
|
I've being using the Vs.net 2002 for about 8 months now. Apart from that, I've tried it in two different OS (XP and WIN2K Pro service 3). For now am even afriad opening it in any other machine running visual studio. I have scanned for virus. The file is not infested with virus. Or does there exist different version of VS.Net 2002?
|
|
|
|
|
Hi, i'd like to learn C#, any book titles suggestion!?
nevhile.net
|
|
|
|
|
Hi there,
I am learning too.
Programming C# 3rd Edition by O'REILLY is pretty good.
|
|
|
|
|
|
Wrox: Professional C# 2nd Edition is fantastic!
Mads
|
|
|
|
|
WebClient objClient= new WebClient();
objClient.DownloadFile("ftp://..../XXXX.txt","c:\\XXXX.txt");
|
|
|
|
|
hi guyz!
actually i have a datset in which i want to filter it according to some condition n then display the required value in the combobox , hope u could help me out , i will be looking forward to ur help thx buddies
Work Hard and Test your Luck
|
|
|
|
|
Use dataset.tables[index].Select to filter the dataset.
I think you should be able to databind the combobox to the dataset using one of it's properties in e.g. designview.
Mads
|
|
|
|
|
Hi everyone
How do get the listview control to force a multiple line display when the
column width is too small?
Anyone written an overide implementation on this? Some sample source will
be most helpful
Many thanks
bchong
|
|
|
|
|
Hi. I want to create a simple notepad.
I used a multiline textbox. I wanted to create a shortcut
for selecting all text in the textbox like pressing ctrl-A would
select all text. Anybody knows how to do this?
Please help me.
"To teach is to learn twice"
|
|
|
|
|
textBox.SelectionStart = 0;
textBox.SelectionLength = textBox.TextLength;
And who modded the parent posting up to 5???
--
-Blake (com/bcdev/blake)
|
|
|
|
|
You can also use textBox.SelectAll().
|
|
|
|
|
I'm new to the C# language, and my Java experience is foiling me here.
My application doesn't really have one main form--it has two (or at least I feel they should be peers). Exactly one of these two windows is visible at any one time, but I'd like the one that IS visible to operate as if it's the main form, in that clicking on the close box of the Form subclass will kill the application. By default, the Form passed to Application.Run(), if closed, kills the entire application. Is there any way to change the main form so that the entire application DOESN'T die just because I call Close() against it. My intent is to create a second (completely different) form, call Show() against it, and then Close() the original one. I could just set it to be invisible, but I'd rather dispose of it entirely and not have to maintain a reference to the Form just so I can call Close() against it when time comes to really kill the application.
Any advice?
|
|
|
|
|
Replace:
Application.Run(myForm);
with:
myForm.Show();
Application.Run();
Alternatively, derive from ApplicationContext and pass one of those into Application.Run(), that's what ApplicationContext's are for.
And don't worry, we won't hold the whole Java thing against you.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Hi there Blake,
i am pretty new to GUI programming and just picked up C#.
is it possible in anyway that you can point my to an example or tutorial with regards to this?
a little more explaination or examples will be Very much appreciated =)
thanks
|
|
|
|
|
With regards to what exactly?
--
-Blake (com/bcdev/blake)
|
|
|
|
|
opps..
i don't really know the term used but.
for example, i have a form that has a couple of controls then when the users click say "Search DataBase", another form will show the status and then finally transit to a Datagrid that shows the database information.
Must i create 3 seperate forms for them and if yes, how do i transit from form to form?
thanks
|
|
|
|