Mike Andrews <[EMAIL PROTECTED]> wrote:
> I've looked at sample code and read up on the switch statement and it
> appears to be unavailable.
> Why is that? If C# is modern language, why does it not have some very
> simple abilities that VB has had?
C# follows a C heritage. As such, operations which don't correspond
(conceptually at least) closely to a machine-level instruction tend to
be more verbose or harder to access. (There are of course more
exceptions in C# these days, with things like anonymous delegates and
lambdas, which are much more feasible to implement in the presence of
garbage collection - but are also very laborious to produce in the
absence of syntax support).
What you're asking for appears to be a switch that can select a branch
based on non-constant expressions. I would think that people with a C
background expect a switch to take constant time to select a branch,
because the branch can use a table or computed lookup. Historically,
some C and C++ compilers have even automatically generated hashtables
for switch statements. Visual Basic-style behaviour is simply an alien
culture.
The nested if-statement, however, is not constant-time - it's linear
time. What's more, not every comparand need be evaluated; if the
comparand expressions have side-effects, execution can be quite
unpredictable.
> It seems that if you want to have one procedure handle the click events for
> several buttons, that doing it this way is harder to read:
>
> ...
> if (sender == btn1){
> ...
One could also add each button to a Dictionary<Control,EventHandler>
map, and it could become:
EventHandler h;
if (map.TryGetValue(sender, out h))
h(sender, e);
But this is simply a long-winded way of reproducing the recommended
approach: simply use different delegates for the Click event on each
button.
-- Barry
--
http://barrkel.blogspot.com/
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com