Sign in to follow this  
Alpha_ProgDes

Should I use protected fields or public properties?

Recommended Posts

Alpha_ProgDes    6921
I have an abstract class Character

[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CW_Project2
{
abstract class Character
{
int HP;
int SP;
int EP;
int Attack;
int Defense;
int Endurance;
int Speed;
int Affinity;
public Character()
{
Attack = 5;
Defense = 5;
Endurance = 5;
Speed = 5;
Affinity = 0;
setBasePoints();
}
public Character(int initialAttack, int initialDefense, int initialEndurance, int initialSpeed, int initialAffinity)
{
Attack = initialAttack;
Defense = initialDefense;
Endurance = initialEndurance;
Speed = initialSpeed;
Affinity = initialAffinity;
setBasePoints();
}
public Character(Attributes loadAttributes, int loadHP, int loadSP, int loadEP)
{
Attack = loadAttributes.Attack;
Defense = loadAttributes.Defense;
Endurance = loadAttributes.Endurance;
Speed = loadAttributes.Speed;
Affinity = loadAttributes.Affinity;
HP = loadHP;
SP = loadSP;
EP = loadEP;
}
private void setBasePoints()
{
Random rnd = new Random();
HP = (((Defense + Endurance) * 10) / 8);
SP = (((Attack + Endurance) * 10) / 7);
EP = (((rnd.Next(10) + Endurance) * 10) / 6);
}
pu
}
}
[/CODE]

And a Player class that inherits Character:

[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CW_Project2
{
class Player : Character
{
long playerID;
CharacterClass playerClass;

public Player()
{
}
public Player(int playerAttack, int playerDefense, int playerEndurance, int playerSpeed, int playerAffinity)
: base(playerAttack, playerDefense, playerEndurance, playerSpeed, playerAffinity)
{

}

public Player(CharacterClass initialClass, int bonusAttack, int bonusDefense, int bonusEndurance, int bonusSpeed, int bonusAffinity)
: this(initialClass.getClassAttributes().Attack + bonusAttack,
initialClass.getClassAttributes().Defense + bonusDefense,
initialClass.getClassAttributes().Endurance + bonusEndurance,
initialClass.getClassAttributes().Speed + bonusSpeed,
initialClass.getClassAttributes().Affinity + bonusAffinity)
{
playerClass = initialClass;
playerID = DateTime.Now.Ticks;
}
}
}
[/CODE]

Now if I want the Player class to access the Characters class private fields, should I just make the fields Protected or create Public Properties? Basically the scenario that I am thinking of is when the Player levels up, I'll need to add points to those attributes.

Share this post


Link to post
Share on other sites
SolarChronus    199
When writing the getters/setters for properties you can specify their access modifier.

Such as:

[CODE]
public int HP
{
get { return HP;}
protected set { HP = value;}
}
[/CODE]

or any combination thereof. The example above allows anyone to access the players HP but only sub-classes can set it. Pretty handy. Edited by SolarChronus

Share this post


Link to post
Share on other sites
Nypyren    12065
In C# 3 and higher, you can use this shorthand to avoid having to create a field in addition to the property.

[code]
public int HP { get; protected set; } // 'public' in this case modifies the getter.
[/code]

Share this post


Link to post
Share on other sites
SiCrane    11839
A protected field in C# is pretty much never the right design choice for pretty much the same reason that a protected member variable in C++ is almost never the right decision: if you have non-public members that means your class has class invariants. If you make a field protected then all derived classes are responsible for maintaining the class invariant, which means that if you have a bug the number of places you need to look for the problem is multiplied significantly.

Share this post


Link to post
Share on other sites
SiCrane    11839
No, the two other posters are talking about properties, not fields. Properties can have code that maintain class invariants attached to them, unlike direct access with fields.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this