+ 1

Problem with a class. C#

So I have my variables set up in my class file, then... public Character(string aCharName, double aCharHpMax, double aCharHpCurrent, double aCharSpeed, double aCharExpMax, double aCharExpCurrent, double aCharDamage, int aCharLevel, bool aIsCombatant) { //Sets up public attributes that we can easily use when creating different characters. charName = aCharName; charHpMax = aCharHpMax; charHpCurrent = aCharHpCurrent; charSpeed = aCharSpeed; charExpMax = aCharExpMax; charExpCurrent = aCharExpCurrent; charDamage = aCharDamage; charLevel = aCharLevel; isCombatant = aIsCombatant; //Keeps track of how many characters we have made. numberOfCharacters++; } The field variables equaling the parameter variables are defined above them with no values assigned. Now... outside of my class file and inside of a form file I have this... private void Form1_Load(object sender, EventArgs e) { //Create Characters //name, hpmax, hpcurr, speed, expmax, expcurrent, damage, level Character pcCharacter = new Character("Protagonist", 20, 20, 5, 100, 1, 5, 1, false); Character rat = new Character("Rat", 5, 5, 3, 100, 1, 2, 1, true); //Set Up Player PlayerName.Text = "Name: " + pcCharacter.aCharName; } It will not let me pull the attributes off of pcCharacter, defined just above it. It's saying that the attribute aCharName is read only, but I don't understand why as I defined it in a public class constructor. Do I have to make a get/set method for this or is there a way to make it MORE public somehow so that I can easily pull that information when I type 'pcCharacter.' or 'rat.' or whatever object I make underneath the 'Character' class. >.<*

14th May 2019, 11:39 PM
Ignatious
1 Respuesta
0
There is some code missing, but I guess there is not such Attribute as you initialize "charName". aCharName is a parameter of your constructor and therefore out of scope at this point in code.
15th May 2019, 5:53 AM
Daniel Adam
Daniel Adam - avatar