Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Compilation failed: 1 error(s), 0 warnings
Program.cs(22,21): error CS1061: Type `SampleRPG.Player' does not contain a definition for `Defense' and no extension method `Defense' of type `SampleRPG.Player' could be found. Are you missing an assembly reference?
Player.cs(9,11): (Location of the symbol related to previous error)


What I have tried:

C#
///Program.cs
///4章

using System;

namespace SampleRPG
{
    class Program
    {
        static void Main(string[] args)
        {
            Player player1 = new Player();
            player1.name = 
            player1.hp = 100;

            Player player2 = new Player();
            player2.name = 
            player2.hp = 50;

            player1.Attack();
            player2.Defense();
        }
    }
}


C#
///Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleRPG
{
    class Player
    {
        public int hp;  // HP
        public int name;

        // 攻撃メソッド
        public void Attack()
        {
            Console.WriteLine("敵の攻撃!");
        }

        // 逃走メソッド
        public void Escape()
        {
            Console.WriteLine("逃げられた!");
        }
    }
}
Posted
Updated 4-Nov-23 4:47am
v2

1 solution

The error message is pretty clear:
Quote:
Type `SampleRPG.Player' does not contain a definition for `Defense'
Take a look at your Player class. Does it define a method called "Defense"? No, it does not.

Your Player class defines two methods, Attack and Escape, nothing else.

There's your problem.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900