Error 12 A field initializer cannot reference the non-static field, method, or property 'WindowsGame1.Player.BaseStrength
I don't understand why it wont let me do the following. Does anyone know a way I can accomplish this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsGame1
{
public class Player
{
int BaseStrength = 10;
int BaseIntelligence = 10;
int BaseDexterity 开发者_运维知识库= 10;
int BaseStamina = 10;
int BaseSpeed = 10;
int Damage;
int SpellDamage;
int Accuracy;
int LifePoints;
int CastingSpeed;
***int Damage = (BaseStrength / 2);***
}
}
The error I get is:
Error 12 A field initializer cannot reference the non-static field, method, or property 'WindowsGame1.Player.BaseStrength
If you want Damage to be a value that is calculated based on other fields you should use a property:
int Damage
{
get
{
return BaseStrength / 2;
}
}
On the other hand, if you want to use an ordinary field and set it once when the object is instantiated you should put the initialization code in the constructor.
public class Player
{
int baseStrength = 10;
int damage;
public Player()
{
damage = baseStrength / 2;
}
}
精彩评论