5 Replies - 3638 Views - Last Post: 27 February 2011 - 08:08 AM Rate Topic: -----

#1 Guest_Rusty*


Reputation:

Need help with error:Non-invocable member cannot be used like a method

Posted 26 February 2011 - 05:54 PM

Hello, I am getting the error: "Non-invocable member ToL.Character.SpellPower cannot be used like a method" while trying to run this code:

public static void CastSpell(Character attacker, Character defender, string spellchoice)
        {
            Spell spell;
            spell = ProcessSpellChoice(spellchoice, attacker);
            int spellpower = spell.SpellCast + attacker.SpellPower(attacker);
            if (spell.isOnSelf == true)
            {
                attacker.CurrentHealth += spellpower;
                if (attacker.CurrentHealth > attacker.MaxHealth)
                {
                    attacker.CurrentHealth = attacker.MaxHealth;
                }
            }
            else if (spell.multipleHits == true)
            {
                defender.CurrentHealth -= spellpower;
                //To Do: make it hit multiple enemies
            }
            else if (spell.singleTarget == true)
            {
                defender.CurrentHealth -= spellpower;
            }
        }


The error occurs at line 5. I have 'SpellPower' defined as a public int in a
seperate file. I read that putting brackets around it should solve
this error but so far I have been unsuccessful. If someone could point me in
the right direction here I would be very
grateful.

Thank you!

Is This A Good Question/Topic? 0

Replies To: Need help with error:Non-invocable member cannot be used like a method

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5631
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Need help with error:Non-invocable member cannot be used like a method

Posted 26 February 2011 - 06:42 PM

View PostRusty, on 26 February 2011 - 06:54 PM, said:

I read that putting brackets around it should solve this error

Did you try that? Because the code you have here doesn't have brackets anywhere in the line.
int spellpower = spell.SpellCast + attacker.SpellPower(attacker);



Without seeing the methods you are trying to use there isn't a lot we can tell you.
Can you provide the code for the
enumeration .SpellCast and the
method .SpellPower()
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Rusty*


Reputation:

Re: Need help with error:Non-invocable member cannot be used like a method

Posted 26 February 2011 - 07:27 PM

Yes I forgot to mention I tried using brackets but it gave me an "identifier expected" error for the word enclosed in brackets on the same line:

public static void CastSpell(Character attacker, Character defender, string spellchoice)
        {
            Spell spell;
            spell = ProcessSpellChoice(spellchoice, attacker);
            int spellpower = spell.SpellCast + attacker.[SpellPower](attacker);
            if (spell.isOnSelf == true)
            {
                attacker.CurrentHealth += spellpower;
                if (attacker.CurrentHealth > attacker.MaxHealth)
                {
                    attacker.CurrentHealth = attacker.MaxHealth;
                }
            }
            else if (spell.multipleHits == true)
            {
                defender.CurrentHealth -= spellpower;
                //To Do: make it hit multiple enemies
            }
            else if (spell.singleTarget == true)
            {
                defender.CurrentHealth -= spellpower;
            }
}


Here is the code you asked for:

class Spell
    {
        public string identifier;
        public Spell()
        {
            multipleHits = false;
            singleTarget = false;
            isOnSelf = false;
            fire = false;
            ice = false;
            lightning = false;
        }

        public virtual int SpellCast(Character caster)
        {
            return power;
        }
    }

}


Was This Post Helpful? 0

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Need help with error:Non-invocable member cannot be used like a method

Posted 26 February 2011 - 08:44 PM

Whats with the SpellCast? is it a function? you would need to call it as a function and it receives a value doesnt it?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Need help with error:Non-invocable member cannot be used like a method

Posted 27 February 2011 - 05:23 AM

public virtual int SpellCast(Character caster)

SpellCast is a member function of the Spell which takes an argument. You are not calling it as a method, and also -- as a direct result -- not passing an argument.

attacker.[SpellPower](attacker)

What's that supposed to do?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5631
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Need help with error:Non-invocable member cannot be used like a method

Posted 27 February 2011 - 08:08 AM

I was actually expecting that to be the case. I tried to hint/nudge the OP with that when I asked this:

Quote

Can you provide the code for the
enumeration .SpellCast and the
method .SpellPower()


Right now the passed in character parameter has no purpose. I'm guessing the OP is knows that eventually he will want to do more and for now is just testing with a fixed value until the rest of the code operates as expected..
public virtual int SpellCast(Character caster)
        {
            return power;
        }



Rusty: I'm not trying to be rude. I'm just trying to gauge your experience. How much C# have you done? I'm going to guess this is your first or second programming experiment. You seem to lack an understanding of when you need to use parameters, or how to form correct syntax for them.Have you worked through a book of exercises or on-line tutorial?

attacker.[SpellPower](attacker);

attacker is an instance of the Character class
Brackets go around an iteration, like specifying the element in an array.
CharacterList[5] would be the 6th character element of the characterlist array
Parenthesis use at the end like you have would be a parameter being sent to a method
But putting it all together like you have is just cyber-gibberish.

You might want to work through a learn C# book from front to back, completely before embarking on your game program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1