is there short way of doing this?

N

.nLL

public static string RatingCalculator(string _input)
{
int input = NvUtils.ReturnNumeric(_input);

if (input < 10)
{
return string.Empty;
}
if (input > 10 && input < 20)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img
src=\"/images/star_empty.png\" alt=\"-\" /><img
src=\"/images/star_empty.png\" alt=\"-\" /><img
src=\"/images/star_empty.png\" alt=\"-\" /><img
src=\"/images/star_empty.png\" alt=\"-\" />";
}
if (input > 21 && input < 40)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img
src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\"
alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img
src=\"/images/star_empty.png\" alt=\"-\" />";
}
if (input > 41 && input < 70)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img
src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\"
/><img src=\"/images/star_empty.png\" alt=\"-\" /><img
src=\"/images/star_empty.png\" alt=\"-\" />";
}
if (input > 11 && input < 120)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img
src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\"
/><img src=\"/images/star.png\" alt=\"*\" /><img
src=\"/images/star_empty.png\" alt=\"-\" />";
}
else
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img
src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\"
/><img src=\"/images/star.png\" alt=\"*\" />";
}
}
 
N

.nLL

from
http://stackoverflow.com/questions/914929/is-there-a-short-way-to-do-this

public static string RatingCalculator(string _input)
{
int input = NvUtils.ReturnNumeric(_input);
string fs = "<img src=\"/images/star.png\" alt=\"*\" />"; //Full
Star
string es = "<img src=\"/images/star_empty.png\" alt=\"-\" />";
//Empty Star
StringBuilder sb = new StringBuilder(fs);
//No need for 'sb.Append (input > 10 ? fs : es);` as we'll test
"input < 10" in th
sb.Append(input > 20 ? fs : es);
sb.Append(input > 40 ? fs : es);
sb.Append(input > 70 ? fs : es);
sb.Append(input > 120 ? fs : es);
return (input < 10) ? string.Empty : sb.ToString();
}
 
J

JM

Create 6 graphics. 1 for each of the possible states.

all empty
1 filled, 4 empty
2 filled, 3 empty
3 filled, 2 empty
4 filled, 1 empty
all filled

Then, it's simply..

public static string RatingCalculator(string _input)
{
int input = NvUtils.ReturnNumber(_input);
If (input < 10)
{ return "<img src=\"/images/zero_stars.png\" alt=\"no stars\" /> }

if (input >= 10 && input < 20)
{ return "<img src=\"/images/one_star.png\" alt=\"one star\" /> }

if (input >= 20 && input < 40)
{ return "<img src=\"/images/two_stars.png\" alt=\"two stars\" /> }
}

... and so on.

You could even split them into halves like a lot of sites do. Also, you
might want to watch your logic on input. As is, you'd be completely skipping
over the numbers 10, 20, 21, 40, 41, 70 and 71..

Pretty simple and a whole lot less code! ;0)

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top