Taking variables from other classes.
-
So I am trying to make a class that uses a constructor of a different class. I use the code (Name of class) Class = new (Name of class(constructor variable)) But oddly the constructor variable won't appear. Could anyone please help?
Your question is not clear. Posting relevant code will help.
Navaneeth How to use google | Ask smart questions
-
Your question is not clear. Posting relevant code will help.
Navaneeth How to use google | Ask smart questions
public class CharClasses { public int healthPoints = 0; public int manaPoints = 0; public int strength = 0; public int dexterity = 0; public int speed = 0; public int intelligence = 0; public CharClasses (string characterClass) { if (characterClass == "Warrior") { healthPoints = 100; manaPoints = 10; strength = 10; dexterity = 5; speed = 3; intelligence = 0; } if (characterClass == "Archer") { healthPoints = 75; manaPoints = 10; strength = 3; dexterity = 10; speed = 5; intelligence = 0; } if (characterClass == "Assassin") { healthPoints = 75; manaPoints = 10; strength = 5; dexterity = 3; speed = 10; intelligence = 0; } if (characterClass == "Mage") { healthPoints = 50; manaPoints = 50; strength = 0; dexterity = 5; speed = 3; intelligence = 10; } } } public class Leveling { public int level = 1; public int experience = 0; public Leveling() { if (experience == ((experience + 100) * 1.5)) { level++; } } } I'm trying to take the constructor named "characterClass" from the class CharClasses and use it in the class Leveling. CharClasses Class = new CharClasses(characterClass); I'm trying to make this code work but the characterClass part of it does not seem to work...
-
public class CharClasses { public int healthPoints = 0; public int manaPoints = 0; public int strength = 0; public int dexterity = 0; public int speed = 0; public int intelligence = 0; public CharClasses (string characterClass) { if (characterClass == "Warrior") { healthPoints = 100; manaPoints = 10; strength = 10; dexterity = 5; speed = 3; intelligence = 0; } if (characterClass == "Archer") { healthPoints = 75; manaPoints = 10; strength = 3; dexterity = 10; speed = 5; intelligence = 0; } if (characterClass == "Assassin") { healthPoints = 75; manaPoints = 10; strength = 5; dexterity = 3; speed = 10; intelligence = 0; } if (characterClass == "Mage") { healthPoints = 50; manaPoints = 50; strength = 0; dexterity = 5; speed = 3; intelligence = 10; } } } public class Leveling { public int level = 1; public int experience = 0; public Leveling() { if (experience == ((experience + 100) * 1.5)) { level++; } } } I'm trying to take the constructor named "characterClass" from the class CharClasses and use it in the class Leveling. CharClasses Class = new CharClasses(characterClass); I'm trying to make this code work but the characterClass part of it does not seem to work...
nik121 wrote:
CharClasses Class = new CharClasses(characterClass); I'm trying to make this code work but the characterClass part of it does not seem to work...
Ok, Have you defined the variable
characterClass
in this scope? What is the error you are getting? :)Navaneeth How to use google | Ask smart questions
-
nik121 wrote:
CharClasses Class = new CharClasses(characterClass); I'm trying to make this code work but the characterClass part of it does not seem to work...
Ok, Have you defined the variable
characterClass
in this scope? What is the error you are getting? :)Navaneeth How to use google | Ask smart questions
-
Yea characterClass was the string for the Constructor in the class CharClasses. But the characterClass says that it doesn't exist in the current context.
nik121 wrote:
Yea characterClass was the string for the Constructor in the class CharClasses. But the characterClass says that it doesn't exist in the current context.
WOW! I am not sure how can I explain this to you. All you can do is to get a book and learn the basics. When you write code like
CharClasses Class = new CharClasses(characterClass)
, compiler is expecting the variablecharacterClass
to be presented in the scope. Here you don't havecharacterClass
declared. You need to declare the variable first before using it. Something likestring characterClass = "Warrior"; CharClasses Class = new CharClasses(characterClass);
:doh:Navaneeth How to use google | Ask smart questions
-
public class CharClasses { public int healthPoints = 0; public int manaPoints = 0; public int strength = 0; public int dexterity = 0; public int speed = 0; public int intelligence = 0; public CharClasses (string characterClass) { if (characterClass == "Warrior") { healthPoints = 100; manaPoints = 10; strength = 10; dexterity = 5; speed = 3; intelligence = 0; } if (characterClass == "Archer") { healthPoints = 75; manaPoints = 10; strength = 3; dexterity = 10; speed = 5; intelligence = 0; } if (characterClass == "Assassin") { healthPoints = 75; manaPoints = 10; strength = 5; dexterity = 3; speed = 10; intelligence = 0; } if (characterClass == "Mage") { healthPoints = 50; manaPoints = 50; strength = 0; dexterity = 5; speed = 3; intelligence = 10; } } } public class Leveling { public int level = 1; public int experience = 0; public Leveling() { if (experience == ((experience + 100) * 1.5)) { level++; } } } I'm trying to take the constructor named "characterClass" from the class CharClasses and use it in the class Leveling. CharClasses Class = new CharClasses(characterClass); I'm trying to make this code work but the characterClass part of it does not seem to work...
If I understand your question,
Leveling
should inheritCharClasses
and use the base constructorcharacterClass
, as follows:Leveling lvl = new Leveling("Warrior");
public class CharClasses
{
public int healthPoints = 0;
public int manaPoints = 0;
public int strength = 0;
public int dexterity = 0;
public int speed = 0;
public int intelligence = 0;public CharClasses(string characterClass) { if(characterClass == "Warrior") { healthPoints = 100; manaPoints = 10; strength = 10; dexterity = 5; speed = 3; intelligence = 0; } if(characterClass == "Archer") { healthPoints = 75; manaPoints = 10; strength = 3; dexterity = 10; speed = 5; intelligence = 0; } if(characterClass == "Assassin") { healthPoints = 75; manaPoints = 10; strength = 5; dexterity = 3; speed = 10; intelligence = 0; } if(characterClass == "Mage") { healthPoints = 50; manaPoints = 50; strength = 0; dexterity = 5; speed = 3; intelligence = 10; } }
}
public class Leveling : CharClasses
{
public int level = 1;
public int experience = 0;public Leveling(string characterClass) : base(characterClass) { if(experience == ((experience + 100) \* 1.5)) { level++; } }
}
If you use inheritance, and don't need to instantiate
CharClasses
directly, you may want to change you your access modifiers from allpublic
toprotected
, etc.Regards, Gary