Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Cannot assign 'Add' because it is a 'method group' (http://www.programmingforums.org/showthread.php?t=14164)

Jarod Silverstar Oct 13th, 2007 10:52 PM

Cannot assign 'Add' because it is a 'method group'
 
Beating my head against the wall on this one. Trying to write a simplified version of a combat routine (to make sure I have all the basic pieces working).

But getting stuck with just getting the basics done and drawing a blank on what I can do to solve this last issue. And hoping I can get some help.

To start with, I have a windows form where some info is entered:
:

namespace BasicCombatForm
{
    public partial class DataEntry : Form
    {
        int[] fighter1 = new int[2];
        int[] fighter2 = new int[2];
       
        public DataEntry()
        {
            InitializeComponent();
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            string nameFighter1 = txtFighter1Name.Text;
            string nameFighter2 = txtFighter2Name.Text;
            fighter1[0] = Convert.ToInt16(txtFighter1Atk.Text);
            fighter1[1] = Convert.ToInt16(txtFighter1Def.Text);
            fighter2[0] = Convert.ToInt16(txtFighter2Atk.Text);
            fighter2[1] = Convert.ToInt16(txtFighter2Def.Text);

            Fight war = new Fight();
            Display show = new Display();
            show.showFight(war.newFight(nameFighter1, nameFighter2, fighter1, fighter2));
        }
    }
}


Next I have the class that handles the combat routine:
:

namespace BasicCombatForm
{
    public class Fight
    {
       
        public string[] newFight(string nameFighter1, string nameFighter2, int[] fighter1, int[] fighter2)
        {
            Random objRan = new Random();
            int roll1 = objRan.Next(1, 21);
            int roll2 = objRan.Next(1, 21);

            int fighter1Atk = fighter1[0] + roll1;
            int fighter1Def = fighter1[1] + roll1;
            int fighter2Atk = fighter2[0] + roll2;
            int fighter2Def = fighter2[1] + roll2;

            string[] result = new string[2];

            if (fighter1Atk > fighter2Def)
                result[0] = nameFighter1 + " scores a hit on " + nameFighter2;
            else
                result[0] = nameFighter1 + " misses " + nameFighter2;

            if (fighter2Atk > fighter1Def)
                result[1] = nameFighter2 + " scorese a hit on " + nameFighter1;
            else
                result[1] = nameFighter2 + " misses " + nameFighter1;

            return result;
        }

    }
}


And last I have a simple output form I want to write the results to:
:

namespace BasicCombatForm
{
    public partial class Display : Form
    {
        public Display()
        {
            InitializeComponent();
        }

        public object showFight(string[] result)
        {
          lstDisplayFight.Items.ToString = result[0];
          lstDisplayFight.Items.Equals = result[1];
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


After all is said and done, the last issue I am having is at the writing to the lstDisplayFight listbox. I keep coming back to the compile error that tells me that .Add or .Equals or any other function I try, cannot assign because it is a 'method group'. And I have not been able to find anything that helps clear up exactly what my issue is.

Am I just going about this the wrong way? Or is there something else I can do? (Of course, I don't even know if the rest of this is going to work either...)

Thanks all.
Jarod

ReggaetonKing Oct 13th, 2007 11:14 PM

You use the Items property and then the Add method.
:

ListBox b = new ListBox();
b.Items.Add(new string("wtf?!?!");

You can also use the Items property as an indexer. To retrieve the items.
:

ListBox b = new ListBox();
...after doing w/e to the listbox
int length = b.Count;
string strArray = new string[length]
for(int x = 0; x < length; x++)
    strArray[x] = b.Items[x]; //gets item in list at index x

For an array, iterate through the array and add then to the listbox.
:

ListBox b = new ListBox();
string[] strArray = new string[10];
...
foreach(string str in strArray)
    b.Items.Add(str);


Hope this helps. Holla if you don't understand.


All times are GMT -5. The time now is 4:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC