views:

129

answers:

6

I feel like this is a pile of you know what I mean. It works but I feel like I'm way overdoing this in terms of the page lifecycle (load and postbacks) and even the redundancy I have in each of the if statements here.

What happens is this:

  1. This method is called on very page load (no matter if postback or whatever)
  2. If the user submits the form it reduces their totalPoints (there's a button below these radiobuttons that allow them to submit and claim points).

So I call this method also right after the users claims point (submits) which drops those points from their total for next time around. So based on the total points in their account, I need to enable/disable these radio buttons after the page refreshes from the last submit

private void SetPointsOptions()
{
    int totalPoints = customer.TotalPoints;

    rbn200Points.Text = "200 pts";
    rbn250Points.Text = "250 pts";
    rbn400Points.Text = "400 pts";
    rbn500Points.Text = "500 pts";
    rbn600Points.Text = "600 pts";

    // clear state of radio buttons & disable submit
    if (totalPoints < 200)
    {
        rbn200Points.Enabled = false;
        rbn250Points.Enabled = false;
        rbn400Points.Enabled = false;
        rbn500Points.Enabled = false;
        rbn600Points.Enabled = false;

        rbn200Points.Checked = false;
        rbn250Points.Checked = false;
        rbn400Points.Checked = false;
        rbn500Points.Checked = false;
        rbn600Points.Checked = false;

        btnClaimRewardPoints.Enabled = false;
        return;
    }

    if(totalPoints >= 200 && totalPoints < 250)
    {
        rbn200Points.Enabled = true;
    }
    else if(totalPoints >= 250 && totalPoints < 400)
    {
        rbn200Points.Enabled = true;
        rbn250Points.Enabled = true;
    }
    else if(totalPoints >= 400 && totalPoints < 500)
    {
        rbn200Points.Enabled = true;
        rbn250Points.Enabled = true;
        rbn400Points.Enabled = true;
    }
    else if(totalPoints >= 500 && totalPoints < 600)
    {
        rbn200Points.Enabled = true;
        rbn250Points.Enabled = true;
        rbn400Points.Enabled = true;
        rbn500Points.Enabled = true;
    }
    else if(totalPoints >= 600)
    {
        rbn200Points.Enabled = true;
        rbn250Points.Enabled = true;
        rbn400Points.Enabled = true;
        rbn500Points.Enabled = true;
        rbn600Points.Enabled = true;
    }
}
+8  A: 

Given that I didn't miss anything vital in your code:

private void SetPointsOptions()
{
    int totalPoints = customer.TotalPoints;
    rbn200Points.Enabled = totalPoints >= 200;
    rbn250Points.Enabled = totalPoints >= 250;
    rbn400Points.Enabled = totalPoints >= 400;
    rbn500Points.Enabled = totalPoints >= 500;
    rbn600Points.Enabled = totalPoints >= 600;
}
Fredrik Mörk
Nope, that will leave lower-point buttons enabled. You need both sides of the range in your rhs expression. ( rbn250Points.Enabled = totalPoints >= 250 )
Jacob
Jacob, from the code, it seems that is waht he is doing...
astander
yea, that makes a little more common sense. Ok, so what about the clearing of the buttons and postback after new points are calculated? It is checking both on page load AND after they click the submit button beacause I am calling this method twice (once on page load in case they don't submit the buttong and come for the first time to the page and another if they do submit the form)
CoffeeAddict
@Jacob: note that each point range also enables the lower-score radio buttons.
Fredrik Mörk
@coffeeaddict: I don't think I would optimize away that second call to the method. It's not performing any heavy work, so I would probably just leave it as it is (unless it is being hit *a lot*). Not enough of an expert in asp.net to give any good suggestions if that is the case, so I'll leave that to others.
Fredrik Mörk
@coffeeaddict in VB.net, you'd use "if page.isPostBack then/else" statement to have it called only once. Not sure what the C# equivalent is.
DA
thanks for the tip.
CoffeeAddict
+1  A: 

Well, you can start by not checking with if else, and use

if (totalpoints >= val) control.enable

which will allow you less repetative enabling

astander
A: 

You can assign multiple properties as follows in C#:

if ( some condition )
{
     rbnA.Enabled = rbnB.Enabled = rbnC.Enabled = rbnD.Enabled = true;
} else {
     rbnA.Enabled = rbnB.Enabled = rbnC.Enabled = rbnD.Enabled = false;
}
Nissan Fan
yea, I don't like chaining like this, it's hard to read.
CoffeeAddict
+2  A: 

I can't speak to how/if/when you want to initiate all that, but there's definite verbosity that can be cut down. For example, that last bit where you enable various buttons can be reduced to this:

if(totalPoints > 200)
{
    rbn200Points.Enabled = true;
}
if(totalPoints > 250)
{
    rbn250Points.Enabled = true;
}
if(totalPoints > 400)
{
    rbn400Points.Enabled = true;
}
 if(totalPoints > 500)
{
    rbn500Points.Enabled = true;
}
if(totalPoints > 600)
{
    rbn600Points.Enabled = true;
}
DA
+2  A: 

OMG, dude. This is an insane amount of code duplication.

I haven't touched C# for a while and do not have VS at hand, but it should go something like this.

var points2buttons = new Dictionary<int, RadioButton>();
points2buttons[200] = rbn200Points;
points2buttons[250] = rbn250Points;
...
foreach (var pointsButton in points2buttons) {
    var button = pointsButton.Value;
    var pts = pointsButton.Key;
    button.Text = pts + " pts";
    button.Checked = totalPoints>pts;
}
...

Using reflection you can even automate populating the dictionary.

yk4ever
no sh** thats why I"m asking for some help
CoffeeAddict
one problem, button does not have a value of what you think. The values are 0,1,2,3. Also, button.Checked = false for some reason is not clearing it on next page load.
CoffeeAddict
+2  A: 

Putting the radio buttons and their associated point values in a Dictionary may help:

// Untested.

int totalPoints = customer.TotalRewardPoints;

var radioButtons = new Dictionary<RadioButton, Int32>();
radioButtons.Add(rbn200Points, 200);
radioButtons.Add(rbn250Points, 250);
radioButtons.Add(rbn400Points, 400);
radioButtons.Add(rbn500Points, 500);
radioButtons.Add(rbn600Points, 600);

foreach (var keyValuePair in radioButtons)
{
  keyValuePair.Key.Text = String.Format("{0} pts", keyValuePair.Value);
  keyValuePair.Key.Enabled = (keyValuePair.Value < totalPoints);
  keyValuePair.Key.Checked = false;
}
Chris R. Timmons
I am not using a RadioButtonList, just a bunch of ASP.NET radiobuttons tied together by a Group property so there is no "value" property that you can use on a single RadioButton control
CoffeeAddict
No no, he's not trying to reference a value on a RadioButton, but rather on the key/value pair object that he's getting from the dictionary. I haven't done VB in a long time, if there was a Dictionary object available then I wasn't familiar with it. But the point is to create an object that pairs each button with a value, and then use a loop to go through them rather than processing each individually.
Jay
ah, yea, makes sense, forgot that is the dictionary value.
CoffeeAddict