Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 03-28-2007, 04:49 PM   #1 (permalink)
Forum Expert
 
Join Date: Dec 2003
Location: Sitting in a chair fulfilling my life's goal
Age: 22
Posts: 2,650
Send a message via AIM to Killamus Send a message via MSN to Killamus
Default 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?
__________________
Procrastinators unite!
Tomorrow.
Saying that Java is nice because it works on all OS's is like saying that anal sex is nice because it works on all genders.
Killamus is offline   Reply With Quote
Old 03-28-2007, 05:24 PM   #2 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

Quote:
Originally Posted by Killamus View Post
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.
TheOutkastDev is offline   Reply With Quote
Old 03-28-2007, 05:39 PM   #3 (permalink)
Forum Expert
 
Join Date: Dec 2003
Location: Sitting in a chair fulfilling my life's goal
Age: 22
Posts: 2,650
Send a message via AIM to Killamus Send a message via MSN to Killamus
Default

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.
__________________
Procrastinators unite!
Tomorrow.
Saying that Java is nice because it works on all OS's is like saying that anal sex is nice because it works on all genders.
Killamus is offline   Reply With Quote
Old 03-28-2007, 05:46 PM   #4 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

Quote:
Originally Posted by Killamus View Post
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?
TheOutkastDev is offline   Reply With Quote
Old 03-28-2007, 11:43 PM   #5 (permalink)
Forum Expert
 
mordero's Avatar
 
Join Date: Nov 2003
Location: Illinois, USA
Age: 22
Posts: 2,911
Default

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

Last edited by mordero; 03-28-2007 at 11:45 PM.
mordero is offline   Reply With Quote
Old 03-29-2007, 12:50 AM   #6 (permalink)
Forum Expert
 
Join Date: Dec 2003
Location: Sitting in a chair fulfilling my life's goal
Age: 22
Posts: 2,650
Send a message via AIM to Killamus Send a message via MSN to Killamus
Default

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.
__________________
Procrastinators unite!
Tomorrow.
Saying that Java is nice because it works on all OS's is like saying that anal sex is nice because it works on all genders.
Killamus is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5