Learning C# syntax coming from javascript and having trouble with certain parts of using classes

Hey guys, I have been coding in javascript recently but decided to try my hand at a 2D turn based RPG a-la Fire Emblem and since every tutorial is in c#, i am currently trying to learn that to make this game. I have a problem with creating classes: I made a base character class that has every stat that I want both player controlled units and enemies to inherit, but with all of the getters and setters and functions in them, I can’t seem to figure out how to pass lists along to child classes and fill them individually for each specific child class. Also, this is my first time on these forums so hopefully I’m not in the wrong section/posting my code wrong!

public class BaseCharacterClass {
    // defining everything to transfer for every character (i.e. stats, description, movement allocation, etc.)

    private string characterName; /* name of each specific character (specific for our characters and country identifiers for enemies (i.e. Jahrian)) */
    private string characterClassName; /* name of character's class (same for enemies and friendlies) */
    private string characterClassDescription; /* description of each character's class (same across the board for friendlies and enemies) */
   // private string validWeapon; /* weapon types that each unit is allowed to wield */
    private string characterDescription; /* description of each individual character (generic for enemies and personalized for our units) */

    private int currentExperience; /* current exp of each individual character */
    private int characterLevel; /* character's current level (Max 20 until promoted) */
    private int lifePointsMax; /* maximum life points for each character */
 
    public List<string> validWeaponTypes = new List<string>();

And this is one of the specific classes that I want to inherit the validWeaponTypes list:

public class BaseKnightClass : BaseCharacterClass {
   
    public void KnightClass() {
        CharacterClassName = "Knight";
        CharacterClassDescription = "A mounted warrior who endured arduous training in order to rid themselves of any glaring weaknesses";
        MovementMax = 7; /* amount of tiles that can be moved w/o accounting for terrain impediments */
        UnitType = "Cavalry"; /* declaring unit type to use in battle calculations */
        validWeaponTypes.Add("sword");
        validWeaponTypes.Add("lance"); /* adding valid weapon types to all instances of a Knight */
        }
}

If any of this is unclear, please let me know so I can clear up any confusion. I left out some of the other stats and stuff from the base class that appear in the child class because it was a lot of unnecessary lines of code. Also, is it better or worse for these variables to be public or private, or does it not make a difference? Thanks in advance guys!

Always default to the least accessible you can get, unless you have a reason to do otherwise. So in this case because you want to access the list from a derived class, go with protected.

As to the OP, I’m not entirely sure what the problem is. What’s not working?

Thanks for the info on private and public, BoredMormon! I guess I was just wondering exactly how to create an instance of the derived(Knight) class in order to check its properties (using my constructor)? I wanted to make sure that the second class had the correct properties for validWeaponTypes but when I plug this code into Visual Studio, nothing I do works for me to create a new instance of the derived class to check. Maybe some of my syntax is off but I think I’m doing it by the book.

You don’t actually have any constructors there, at least not in the code posted. If you intended for

public void KnightClass() {}

to be your constructor it is a bit off. It should be

public BaseKnightClass(){}

Thanks ThermalFusion, so I can create a new instance of BaseKnightClass inside the overarching BaseKnightClass now, but for some reason when I try to check its property values it doesn’t work? Something like this:

    public class BaseKnightClass : BaseCharacterClass {
     
        public BaseKnightClass() {
            CharacterClassName = "Knight";
            CharacterClassDescription = "A mounted warrior who endured arduous training in order to rid themselves of any glaring weaknesses";
            MovementMax = 7; /* amount of tiles that can be moved w/o accounting for terrain impediments */
            UnitType = "Cavalry"; /* declaring unit type to use in battle calculations */
            validWeaponTypes.Add("sword");
            validWeaponTypes.Add("lance"); /* adding valid weapon types to all instances of a Knight */
            }
BaseKnightClass knight1 = new BaseKnightClass();
    }

Console.WriteLine(knight1.validWeaponTypes)

Define “doesn’t work”.

Basically, i think this should be creating a new instance of the BaseKnightClass called knight1, right? And then when I try to print out the values assigned to this object’s validWeaponTypes list, it says that “knight1 doesn’t exist in the current context”. So, I think i created the list right, created a derived class right, fixed the constructor, but I somehow can’t figure out what I’m doing wrong to double check these values :confused:

Where exactly is that Console.Writeline being called from? I’m assuming it’s not just sitting outside the class like you have it pasted there because that would not compile. And is knight1 actually inside the BaseKnightClass as it appears here? That seems like it would create an infinite loop the instant you create an instance of one of these.

Put both of those lines somewhere else(like in a new script’s Start() function for example), and in the same place so that it knight1 is in the same scope.

1 Like