if else

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

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<User> userList;
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
 
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
 
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,
ActionDelegate> can work the same.
You still have to fill it though.
 
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


message
 
'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, Action> is 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]
 
'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.
 
'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
 
Back
Top