Click here to Skip to main content
15,913,854 members
Home / Discussions / C#
   

C#

 
AnswerRe: Make a sql query object by C#. Pin
OriginalGriff14-Dec-11 21:35
mveOriginalGriff14-Dec-11 21:35 
GeneralRe: Make a sql query object by C#. Pin
Vincet Shen15-Dec-11 22:11
Vincet Shen15-Dec-11 22:11 
QuestionEvent Handler for button clicks Pin
Miwin Solutions14-Dec-11 7:43
Miwin Solutions14-Dec-11 7:43 
AnswerRe: Event Handler for button clicks Pin
Luc Pattyn14-Dec-11 8:28
sitebuilderLuc Pattyn14-Dec-11 8:28 
GeneralRe: Event Handler for button clicks Pin
Miwin Solutions14-Dec-11 9:24
Miwin Solutions14-Dec-11 9:24 
GeneralRe: Event Handler for button clicks Pin
Miwin Solutions14-Dec-11 22:46
Miwin Solutions14-Dec-11 22:46 
GeneralRe: Event Handler for button clicks Pin
Luc Pattyn14-Dec-11 23:41
sitebuilderLuc Pattyn14-Dec-11 23:41 
AnswerRe: Event Handler for button clicks Pin
BillWoodruff14-Dec-11 21:42
professionalBillWoodruff14-Dec-11 21:42 
Here's another alternative:
C#
// assume this Click EventHandler is used by several buttons. I also assume you are going to want to execute different code for each different Button's Click Event.

private void ButtonsForWhatEver_Click(object sender, EventArgs e)
{
    switch((sender as Button).Name)
    {
        case "btn_whatEver1":
            // call a method with code common to all Buttons here
            // call code for this specific Button here
            break;
        case "btn_whatEver2":
            // call a method with code common to all Buttons here
            // call code for this specific Button here
            break;
        case "btn_whatEver4":
            // call a method with code common to all Buttons here
            // call code for this specific Button here
            break;
        default:
            // you reach here if you have not
            // had a match on a Button's name
            // ignore ? throw an error ? put up a Dialogue ?
            //
            // my choice would be to throw an error during development:
            // force yourself to correct a mis-call sooner rather than later
            break;
    }
}
Discussion:

1. there are endless discussions here on CP, and StackOverFlow, about the relative efficiency of using a string in a switch statement compared to a numeric value. For example:[^]

1.a. if your "buttons are not being pushed" a thousand times a minute Smile | :) , in this case using a string will probably have an insignificant performance hit.

1.b. what the compiler does in translating structs into IL is quite complex: with a switch statement with many string conditions, I read that it will generate and use a hash-table. In other circumstances, I read, a Dictionary is used "under the hood." For an in-order sequence of integers: the most performant possible jump-table is created, I read.

2. In a Button Click EventHandler you really don't need to worry that "sender" is going to be "null," or not a Button: code that would invoke a Button EventHandler directly, without a Button being pushed, would be an abomination Smile | :)

2.a. If you did have code in a Button Click EventHandler that needed to be used from outside the BC EH, then that code should be broken out into its own method, and then the BC EH can call that method.

best, Bill
When I consider the brief span of my life, swallowed up in the eternity before and after, the little space which I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which knows me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.
          Blaise Pascal


modified 15-Dec-11 3:58am.

GeneralRe: Event Handler for button clicks Pin
Miwin Solutions14-Dec-11 22:49
Miwin Solutions14-Dec-11 22:49 
GeneralRe: Event Handler for button clicks Pin
SilimSayo15-Dec-11 4:05
SilimSayo15-Dec-11 4:05 
AnswerRe: Event Handler for button clicks Pin
Luc Pattyn15-Dec-11 5:57
sitebuilderLuc Pattyn15-Dec-11 5:57 
AnswerRe: Event Handler for button clicks Pin
BillWoodruff15-Dec-11 14:20
professionalBillWoodruff15-Dec-11 14:20 
QuestionC# VS10 and error "no suitable method to override" issue Pin
Deborah Palmer McCain14-Dec-11 6:27
Deborah Palmer McCain14-Dec-11 6:27 
AnswerRe: C# VS10 and error "no suitable method to override" issue Pin
Richard MacCutchan14-Dec-11 22:23
mveRichard MacCutchan14-Dec-11 22:23 
GeneralRe: C# VS10 and error "no suitable method to override" issue Pin
Deborah Palmer McCain15-Dec-11 5:27
Deborah Palmer McCain15-Dec-11 5:27 
AnswerRe: C# VS10 and error "no suitable method to override" issue Pin
BillWoodruff15-Dec-11 14:30
professionalBillWoodruff15-Dec-11 14:30 
GeneralRe: C# VS10 and error "no suitable method to override" issue - books for beginners Pin
Deborah Palmer McCain15-Dec-11 16:35
Deborah Palmer McCain15-Dec-11 16:35 
GeneralRe: C# VS10 and error "no suitable method to override" issue - books for beginners Pin
BillWoodruff15-Dec-11 18:40
professionalBillWoodruff15-Dec-11 18:40 
GeneralRe: C# VS10 and error "no suitable method to override" issue - books for beginners Pin
Deborah Palmer McCain15-Dec-11 20:37
Deborah Palmer McCain15-Dec-11 20:37 
QuestionShowDialog not displaying form. Pin
mnaveed14-Dec-11 6:23
mnaveed14-Dec-11 6:23 
AnswerRe: ShowDialog not displaying form. Pin
BillWoodruff14-Dec-11 22:03
professionalBillWoodruff14-Dec-11 22:03 
QuestionError 1067 C# windows service Pin
eyesark14-Dec-11 4:07
eyesark14-Dec-11 4:07 
AnswerRe: Error 1067 C# windows service Pin
PIEBALDconsult14-Dec-11 4:34
mvePIEBALDconsult14-Dec-11 4:34 
AnswerRe: Error 1067 C# windows service Pin
Richard MacCutchan14-Dec-11 5:58
mveRichard MacCutchan14-Dec-11 5:58 
AnswerRe: Error 1067 C# windows service Pin
jschell14-Dec-11 9:18
jschell14-Dec-11 9:18 

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.