473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

if else

Hi

I have a property (item.Id) which contains a Guid. Based on the value
of this Guid I have to execute one of several different tasks. The
tasks are really all variations on the theme "obtain user data from a
database", and at the moment these "tasks" are just methods I call in
my class.

What is a nice syntax for this type of requirement? At the moment I
just use a big if-else (like that below), calling the appropriate
method, but I can't help but believe there's some more elegant
structure for this.

IList<UseruserList;
if (item.Id == guid_1)
{
userList = FindUsers_1();
}
else if (item.Id == guid_2)
{
userList = FindUsers_2();
}
else if (item.Id = guid_3)
{
userList = FindUsers_3();
}
else
{
userList = FindUsers_X();
}
Thanks,
Peter
Oct 2 '08 #1
7 2160
Maybe a dictionary of Guid to delegate instances (such as Action)?

var actions = new Dictionary<Guid, Action>
{
{guid1, SomeMethod},
{guid2, SomeOtherMethod}
//...
};
Action action;
if (actions.TryGetValue(item.Id, out action))
{
action(); // invoke
}

Obviously you might choose to cache the dictionary somewhere rather than
re-create it each time...

Marc
Oct 2 '08 #2
On Oct 2, 8:20 am, "Peter" <xdz...@hotmail.comwrote:
Hi

I have a property (item.Id) which contains a Guid. Based on the value
of this Guid I have to execute one of several different tasks. The
tasks are really all variations on the theme "obtain user data from a
database", and at the moment these "tasks" are just methods I call in
my class.

What is a nice syntax for this type of requirement? At the moment I
just use a big if-else (like that below), calling the appropriate
method, but I can't help but believe there's some more elegant
structure for this.
It depend of the way you generate the list.
If the guid are always going to be the same (it seems) and the tasks
are harcoded you can use a switch instead of a number of if/else.
If the task can change you could create a dictionary with a <guid,
ActionDelegatecan work the same.
You still have to fill it though.
Oct 2 '08 #3
'cos it takes less typing than Dictionary<Guid, Action;-p

Obviously it would work fine either way (in C# 3.0, at least) - but to
my view there is no information lost by using "var" here - it is
immediately obvious (from the declaration/initialization) that the
variable is a Dictionary<Guid, Action>. Note that I wouldn't do this
with "int" etc - but Dictionary<Guid, Actionis a lot of characters
for the eye...

The only time var is strictly /necessary/ is when using anonymous
types - but it has other uses, especially with complex generic
collections - the names get very wordy, without adding anything
(caveat: subjective).

Marc [C# MVP]
Oct 3 '08 #4
On Oct 3, 7:28*am, Marc Gravell <marc.grav...@gmail.comwrote:
'cos it takes less typing than Dictionary<Guid, Action;-p
Absolutely. And it's not just because we're lazy (although that is a
good reason). I think it enhances readability especially in cases
where it reduces lines that would normally scroll off the end of the
screen down to half their length.
Oct 3 '08 #5
One doubt

are we getting any performance improvement from moving to Switch instead of
using If/else ?

implementation of switch statement uses if/else only isn't ?

Thanks
Jibesh
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:f3**********************************@x35g2000 hsb.googlegroups.com...
On Oct 2, 8:20 am, "Peter" <xdz...@hotmail.comwrote:
>Hi

I have a property (item.Id) which contains a Guid. Based on the value
of this Guid I have to execute one of several different tasks. The
tasks are really all variations on the theme "obtain user data from a
database", and at the moment these "tasks" are just methods I call in
my class.

What is a nice syntax for this type of requirement? At the moment I
just use a big if-else (like that below), calling the appropriate
method, but I can't help but believe there's some more elegant
structure for this.

It depend of the way you generate the list.
If the guid are always going to be the same (it seems) and the tasks
are harcoded you can use a switch instead of a number of if/else.
If the task can change you could create a dictionary with a <guid,
ActionDelegatecan work the same.
You still have to fill it though.

Oct 3 '08 #6
On Thu, 02 Oct 2008 18:05:23 -0700, Jibesh <ji*******@gmail.comwrote:
One doubt

are we getting any performance improvement from moving to Switch instead
of
using If/else ?

implementation of switch statement uses if/else only isn't ?
Not necessarily. Depending on the cases, different strategies can be
used. In the best case, a simple jump table can be used, providing much
better efficiency than a sequence of if/else statements.

That said, IMHO the most important part of using a switch statement is to
preserve the semantics of the problem. If what you have is a series of
possibilities that can be encoded in a switch statement, then the switch
should be used. That would be true even if, for some bizarre reason, the
compile switch statement was _slower_ than the if/else statements.

The performance is a consideration that's a distance second to making the
code readable and maintainable. One should only worry that much about it
if and when there's a demonstrable performance problem that needs
resolving.

Pete
Oct 3 '08 #7
"Brian Gideon" <br*********@yahoo.comwrote in message
news:68**********************************@z66g2000 hsc.googlegroups.com...
On Oct 3, 7:28 am, Marc Gravell <marc.grav...@gmail.comwrote:
'cos it takes less typing than Dictionary<Guid, Action;-p
Absolutely. And it's not just because we're lazy (although that is a
good reason). I think it enhances readability especially in cases
where it reduces lines that would normally scroll off the end of the
screen down to half their length.

Yep yep. Also using the var aligns local variables pretty neatly.
Very good for readability

- Michael Starberg
Oct 7 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
3
by: Patrice | last post by:
Hi, I need to do multi-conditional statements like below, but this error is displayed : Expected 'End' /myFilepath, line x else response.write(arrCorpo(sparam,sdiv)) end if I don't...
5
by: WindAndWaves | last post by:
Hi Team The function below searches all the tables in a database. However, if subsearch = true then it searches all the objects listed in a recordset (which are all table names). I thought to...
5
by: Brie_Manakul | last post by:
Is there a way in javascript to do an if else that shows a script in an iframe? Let me know if that doesn't make sense. We have a portal and in a portlet I need to grab these javascript links to...
4
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*,...
8
by: pelicanstuff | last post by:
Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile? All the Elses and Ifs look ok to me but there's a few. Private Sub...
23
by: bearophileHUGS | last post by:
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other...
17
by: JRough | last post by:
I'm trying to get error proof code. I have this code which seems to work but now I look at it I think it should be elseif not else and I wonder why it works. It is in the block:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.