Jump to content

  • Log In with Google      Sign In   
  • Create Account


Should I use protected fields or public properties?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Alpha_ProgDes   Crossbones+   -  Reputation: 4680

Like
0Likes
Like

Posted 03 November 2012 - 05:44 PM

I have an abstract class Character

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
    }
}

And a Player class that inherits Character:

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;
	    }
    }
}

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.
Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

Sponsor:

#2 SolarChronus   Members   -  Reputation: 199

Like
2Likes
Like

Posted 03 November 2012 - 07:15 PM

When writing the getters/setters for properties you can specify their access modifier.

Such as:

public int HP
{
	 get { return HP;}
	 protected set { HP = value;}
}

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, 03 November 2012 - 07:15 PM.


#3 Nypyren   Crossbones+   -  Reputation: 3673

Like
3Likes
Like

Posted 03 November 2012 - 08:25 PM

In C# 3 and higher, you can use this shorthand to avoid having to create a field in addition to the property.

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


#4 SiCrane   Moderators   -  Reputation: 9386

Like
1Likes
Like

Posted 04 November 2012 - 08:40 AM

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.

#5 Alpha_ProgDes   Crossbones+   -  Reputation: 4680

Like
0Likes
Like

Posted 04 November 2012 - 12:28 PM

So what would be the right/correct approach for my problem? Because it seems like you are disagreeing with the solutions above.
Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#6 SiCrane   Moderators   -  Reputation: 9386

Like
0Likes
Like

Posted 04 November 2012 - 01:00 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS