RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Why doesn't this code work?

Killamus

Knight
Why doesn't this code work?

Code:
            foreach (RadioButton ctrl1 in panel1.Controls)
            {
                RadioButton rb1;
                List<RadioButton> rbs = new List<RadioButton>();
                if ((RadioButton)ctrl1.Checked = true)
                {
                    rbs.Add(ctrl1);
                    rb1 = rbs.IndexOf(0);
                        string text;
                        text = rb1.Text;
                        Affin type1 = (Affin)Enum.Parse(typeof(Affin), text, true);
                        type1 = attacker.Affinity;
                        Continue = true;
                        textBox1.Text = "1";
                }
            }
It doesn't set the affinity correctly. It writes "1" though. I can't figure it out. Also, is this the correct code to do this?
 
Killamus;667957 said:
Code:
            foreach (RadioButton ctrl1 in panel1.Controls)
            {
                RadioButton rb1;
                List<RadioButton> rbs = new List<RadioButton>();
                if ((RadioButton)ctrl1.Checked = true)
                {
                    rbs.Add(ctrl1);
                    rb1 = rbs.IndexOf(0);
                        string text;
                        text = rb1.Text;
                        Affin type1 = (Affin)Enum.Parse(typeof(Affin), text, true);
                        type1 = attacker.Affinity;
                        Continue = true;
                        textBox1.Text = "1";
                }
            }
It doesn't set the affinity correctly. It writes "1" though. I can't figure it out. Also, is this the correct code to do this?

I'm not sure what your code should be doing; but off the bat, I see some funky behavior.

Code:
List<RadioButton> rbs = new List<RadioButton>();

For each radio button the panel's controls, you allocate a List of type RadioButton. This goes out of scope after each pass of iterator, and is GCed. Are you positive that shouldn't be outside the for loop?

If so, you might want to re-examine

Code:
rbs.Add(ctrl1);
                    rb1 = rbs.IndexOf(0);

That will return the RadioButton ctrl references every time, making it redundant.
 

Killamus

Knight
Code:
            List<RadioButton> radbutton = new List<RadioButton>();
            foreach (RadioButton ctrl1 in panel1.Controls)
            {
                radbutton.Add(ctrl1);
                foreach (RadioButton ctrl in radbutton)
                {
                    if (ctrl.Checked)
                    {
                        string text;
                        text = ctrl.Text;
                        Affin type1 = (Affin)Enum.Parse(typeof(Affin), text, true);
                        type1 = attacker.Affinity;
                        Continue = true;
                        textBox1.Text = "1";
                    }
                }
            }
Still doesn't work. And I basically want to take a selected radiobutton's text and change it to an enum.
It doesn't crash when I do it; It just doesn't set the affinity correctly.
 
Killamus;667983 said:
Affin type1 = (Affin)Enum.Parse(typeof(Affin), text, true);
type1 = attacker.Affinity;

You parse Affinity with the text, but then overwrite that with attacker.Affinity? Should that be reversed?
 

mordero

Knight
Ok, not sure if this is what you are looking for, but this works and does what I think you need:
Code:
List<RadioButton> rbs = new List<RadioButton>();
       void CheckRadioButtons()
        {
            foreach (RadioButton rb in panel1.Controls)
            {
                if (rb.Checked == true)
                {
                    rbs.Add(rb);
                    string text = rb.Text;
                    Affin type = (Affin)Enum.Parse(typeof(Affin), text, true);
                    return;
                }
            }
        }

Once it finds a radio button that is checked, it will return because that means no others can be checked. It should do what you want (I think) :)

And im curious to what this list of radio buttons is for since there really isnt a reason to store them in a list since only one will ever be checked...

Also, what Outkast said is also a problem with your code :)
 

Killamus

Knight
I really didn't know how else to do it. But I got it fixed, every code there worked, but the
Code:
type1 = attacker.Affinity;
was backwards, so it was never set.
I feel so stupid at times.

Is there a better way to find which one of them was checked? I couldn't think of one.
 
Top