|
I agree with OriginalGriff.
I'd use switch every time. The conditional operator is fine for small in line statements, but nesting it like that just makes it unreadable too quickly.
The switch is much more readable.
Simon
|
|
|
|
|
Hi,
if it is just one matching you can go with approach 'B'
otherwise i will prefer the First Approach 'A'
because the Second Type has to be parsed as first and process.
and Even if it is more than 2 matching it is not good practice to use 'B'
because it is not much readable and understandable.and for each case we can
make a comment(purpose) in 'A', But it is not suitable for 'B'
but anyway it depends on the place where use, and the situation where you use,and even the appearance where you use
thanks
|
|
|
|
|
It depends on the complexity of the case. I'd definitely avoid the second condition because debugging nested ternary's is an absolute nightmare. In a more complex case than this though, I'd drop both constructs in favour of an Action or Func. Have a look at the Log class in my article here[^] to see how it works.
The basic approach is to set up a set of Actions (or Funcs) that correspond to the operations in your switch statement and assign them to a dictionary (the key in the dictionary is the value for your switch statement). All you need do then is trigger the appropriate action based on the key. In the log sample, this line actually performs the validation:
_actions[level](message);
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I'll go for Switch because its easy to track errors in that.
|
|
|
|
|
@OriginalGriff
Fixed the switch for ya
clarity and readability are much more important than saving space on screen.
Agreed 100%
Which is why I posted - As the ?: was/is very clear to me, and I can at a glance understand what's going on, but I'm well aware it's not a construct used that/as often, so wanted to test the waters with other coders.
-debugging nested ternary's is an absolute nightmare
-I'll go for Switch because its easy to track errors in that.
Good point
As this is a *very* simple case I didn't even think of that actually.
But it might need to get more complicated as the code evolves.
Where I'm using it, it's not very likely though - But those are "famous last words", so point taken and agreed to.
--
Thank you all for the feed back - I'll tidy up my code now
|
|
|
|
|
As others said switch is more readable in this case. Also when switch has more labels, compilers use a lookup table (probably hash tables) to implement that and it will yield in accessing all the labels in constant time.
Best wishes,
Navaneeth
|
|
|
|
|
The swtich statement is more readable here, but I think in general swtiches give of a bad code smell:clickety[^]
Especially if they are large or used repeatedly.
CCC solved so far: 2 (including a Hard One!)
|
|
|
|
|
In my opinion, a switch on a string value is definitely a code smell. But using if/else or the ternary operator simply to avoid a switch is worse.
|
|
|
|
|
|
No, it's definitely about readability.
|
|
|
|
|
mgkr wrote: What would you use (and why)?
neither, because
1) needing such a conversion often indicates a bad design e.g. a bad choice of data structuring;
2) and when unavoidable, i.e. when I do need to map characters to small positive integers, I use a snippet that is based on "literalString".IndexOf(char)
PS: at least two replies mentioned readability, which does not even appear (any more?) in the OP.
I hate it when the OP gets edited, and yes readability is very important.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
|
Neither.
public enum IDenum { None , a , b , c }
...
IDenum ID = System.Enum.Parse ( typeof(IDenum) , whatever ) ;
(Except I use my own TryParse method.)
|
|
|
|
|
How to lock the column width expansion in list view at run time
anu gunturi
|
|
|
|
|
Hi
Just a simple approach, but not feasible,
see bellow code
private void listView1_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
{
if(listView1.Columns[e.ColumnIndex].Width != 50)
listView1.Columns[e.ColumnIndex].Width = 50;
}
check the static column width, if it miss matches then it is changed so reset it , this is a simple approach, if you find some other good method follow that.
thanks
|
|
|
|
|
Actually this would a little be better:
private void listView1_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
{
e.Cancel = true;
e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
}
|
|
|
|
|
Hi all
Can some one lead me for a good logic to generate the regular expression dynamically.
Example:
I give an input like bellow
1. Us phone no - (910)456-8970 => regular expression is: @"\(\d{3}\)\s\d{3}-\d{4}"
2. Email Id - miltoncse00@yahoo.com => string pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
@"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" +
@"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
My Need,
By getting (910)456-8970 this string i want to construct the regular expression like @"\(\d{3}\)\s\d{3}-\d{4}"
and i have to do for all the string vice versa. Can some tell me some techniques or logic to incorporate.
My Real Problem With Regular Expression :
I have to mask the fields in website based on the pattern like usphone,emailid etc...
thanks in Adavance
|
|
|
|
|
I doubt that you can (correctly) deduce a regex from sample data. Take the phone-number column as an example, with this data;
555-423325 What mask would the first three five's generate, and would that mask be reusable as a phone-number-mask?
It gets worse, since the generated mask isn't a phone-number-mask (even if you base it on the data in that column) - it will be a mask that allows all data within that column (since that's what it's based on).
The best approach would be to make a list of examples for each column, containing correct and incorrect values. Next, you base the regex on the domain of the values that you expect. Lastly, a unit-test to verify that the regex works
I are Troll
|
|
|
|
|
Hi thanks for your reply,
The mask is not only for a corporate sites, it is for the entire web.
if i specify to mask all the security No (SSN) in a page, even if i Google it also, i have to mask the SSN in the Google page as (XXXXXXXX). I have done that using ihtmlElementCollection (BHO). My problem is i know SSN format so there is no issue.But there can be any format, so just seeing one format dynamically i want to generate the regex. This question may be quiet meaningless, but we can could arrive a solution. if no one had come across like this, then i will try to generate a algorithm to achieve, but my thinking is, if already some one knows on that, then i don't want to wast my time to creating algorithm again.
thanks any suggestions are greatfull
Thanks
|
|
|
|
|
jasome wrote: But there can be any format, so just seeing one format dynamically i want to generate the regex.
Your application encounters the string "8572398947". Is that my SSN (being Dutch), or my SSN + 1, or just a random number? How would you know that the string is in fact, a SSN if you cannot verify that it is?
I are Troll
|
|
|
|
|
Hi,
Good exactly you are correct,
sorry for taking the SSN no instead take USPHone No like ... etc..
added to that for SSN there is option to mask based on the Field value, for
example if the input/span element contain SSN then i mask that.
This regex is only for unique analyzing
for example if i give :
user@gmail.com - we can make regular expression for all the mails,
Thanks for your continuous encouragement
you are most welcome to make a comment on that....
|
|
|
|
|
jasome wrote: for example if i give :
user@gmail.com - we can make regular expression for all the mails,
So taking this example, you take that input and generate an "email address" regex from it. Now I try to enter my email address (which is .co.uk) and whoops, it fails. Yet mine is a totally valid email address.
There is absolutely no way you can make a regular expression from input data and have it work in all cases.
|
|
|
|
|
jasome wrote: user@gmail.com - we can make regular expression for all the mails,
That's assuming that the computer can recognize this as a valid email-address. The fastest validation would be done over a regex. (That's recursion, right there)
In other words; you don't know if the data is valid - thus any regex that's derived from it would be a good fit for the sample data, but not for the mask that you want.
I are Troll
|
|
|
|
|
hi to all.....
plz help me in....how to design page navigation window form using back and next button......for exmple...if we take a window like install wizard window..in that v use a button name as next.. if we clik that next ..the next page ill be open ...like ....sooo can u any one send me that source code...
@nu Gunturi
|
|
|
|
|
Don't ask for source code. Present your problem and how you've tried to solve it and include any error messages / exceptions etc.
To answer your question: if you want to create something like a WinForm wizard, open the "next step" form when clicking the "Next" button and close the old form (or hide it if the user wants to go back). Pass on any paramteters you might be needing.
|
|
|
|