Click here to Skip to main content
15,914,642 members
Home / Discussions / C#
   

C#

 
GeneralRe: Clipboard paste formated text in Word Pin
juliogyn13-Aug-13 2:23
juliogyn13-Aug-13 2:23 
GeneralRe: Clipboard paste formated text in Word Pin
OriginalGriff13-Aug-13 3:52
mveOriginalGriff13-Aug-13 3:52 
GeneralRe: Clipboard paste formated text in Word Pin
juliogyn13-Aug-13 7:59
juliogyn13-Aug-13 7:59 
GeneralRe: Clipboard paste formated text in Word Pin
OriginalGriff13-Aug-13 8:04
mveOriginalGriff13-Aug-13 8:04 
GeneralRe: Clipboard paste formated text in Word Pin
juliogyn13-Aug-13 8:58
juliogyn13-Aug-13 8:58 
QuestionConditional question Pin
vkEE12-Aug-13 8:32
vkEE12-Aug-13 8:32 
GeneralRe: Conditional question Pin
PIEBALDconsult12-Aug-13 11:01
mvePIEBALDconsult12-Aug-13 11:01 
AnswerRe: Conditional question Pin
BillWoodruff12-Aug-13 19:28
professionalBillWoodruff12-Aug-13 19:28 
Try:
C#
private string makeMessage(int value)
{
    // note that 'return is one valid way to exit a 'case block
    switch (value)
    {
        case 0:
            return "message: input is #0";
        case 1:
            return "message: input is #1";
        case 2:
            return "message: input is #2";
        default:
            Exception iEX = new Exception("MakeMessage: Input out of range");
            MessageBox.Show(iEX.Message);
            return "Kablooey !";
            // or throw the Exception?
    }
}
And, then test it like this:
C#
string x = makeMessage(0);
string y = makeMessage(1);
string z = makeMessage(2);

string makeAnError = makeMessage(5);
More efficient" ? My understanding is that a switch-case block that uses an ordinal sequence of regularly incremented integer indexes is compiled into IL with maximum efficiency to essentially be a series of jump-table calls.

Your code will not compile as is because of a potential type-mismatch: so in the sense it can't possibly work now, as is, yes, it could be better Smile | :)

Assuming the variable 'value is an integer:

If 'value is an integer, 1, or 2, then a string is returned, but if not, then the compiler "sees" that potentially an integer value could be returned, which is why it won't compile: you "promised" to return a string.

The extent of input validation you may need/want to implement, imho, will depend on the certainty you have that that code will always be called with an integer argument.

If you are absolutely certain this code is always called with its argument taken from a database field which is guaranteed to contain an integer whose value is #0, #1, or #2, sure, your code is fine ... if you add in something to handle the case where the input is #0.

~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal


modified 13-Aug-13 20:42pm.

AnswerRe: Conditional question Pin
V.12-Aug-13 21:05
professionalV.12-Aug-13 21:05 
GeneralRe: Conditional question Pin
BillWoodruff13-Aug-13 6:33
professionalBillWoodruff13-Aug-13 6:33 
GeneralRe: Conditional question Pin
V.13-Aug-13 19:41
professionalV.13-Aug-13 19:41 
GeneralRe: Conditional question Pin
BillWoodruff14-Aug-13 4:03
professionalBillWoodruff14-Aug-13 4:03 
GeneralRe: Conditional question Pin
vkEE13-Aug-13 6:56
vkEE13-Aug-13 6:56 
GeneralRe: Conditional question Pin
BillWoodruff13-Aug-13 7:14
professionalBillWoodruff13-Aug-13 7:14 
AnswerRe: Conditional question Pin
V.13-Aug-13 19:48
professionalV.13-Aug-13 19:48 
GeneralRe: Conditional question Pin
Forbiddenx14-Aug-13 8:30
Forbiddenx14-Aug-13 8:30 
GeneralRe: Conditional question Pin
BillWoodruff24-Aug-13 14:10
professionalBillWoodruff24-Aug-13 14:10 
AnswerRe: Conditional question Pin
Forbiddenx14-Aug-13 8:17
Forbiddenx14-Aug-13 8:17 
QuestionLicensing Hardware Pin
Bram van Kampen11-Aug-13 15:23
Bram van Kampen11-Aug-13 15:23 
AnswerRe: Licensing Hardware Pin
Bernhard Hiller11-Aug-13 22:11
Bernhard Hiller11-Aug-13 22:11 
AnswerRe: Licensing Hardware Pin
Ron Beyer12-Aug-13 16:35
professionalRon Beyer12-Aug-13 16:35 
Questionlogin form Pin
Member 1020296011-Aug-13 12:15
Member 1020296011-Aug-13 12:15 
AnswerRe: login form Pin
Mycroft Holmes11-Aug-13 12:45
professionalMycroft Holmes11-Aug-13 12:45 
GeneralRe: login form Pin
Member 1020296012-Aug-13 6:25
Member 1020296012-Aug-13 6:25 
GeneralRe: login form Pin
Mycroft Holmes12-Aug-13 12:41
professionalMycroft Holmes12-Aug-13 12:41 

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.