Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Taking variables from other classes.

Taking variables from other classes.

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nik121
    wrote on last edited by
    #1

    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?

    N 1 Reply Last reply
    0
    • N nik121

      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?

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Your question is not clear. Posting relevant code will help.

      Navaneeth How to use google | Ask smart questions

      N 1 Reply Last reply
      0
      • N N a v a n e e t h

        Your question is not clear. Posting relevant code will help.

        Navaneeth How to use google | Ask smart questions

        N Offline
        N Offline
        nik121
        wrote on last edited by
        #3

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

        N G 2 Replies Last reply
        0
        • N nik121

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

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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

          N 1 Reply Last reply
          0
          • N N a v a n e e t h

            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

            N Offline
            N Offline
            nik121
            wrote on last edited by
            #5

            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.

            N 1 Reply Last reply
            0
            • N nik121

              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.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              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 variable characterClass to be presented in the scope. Here you don't have characterClass declared. You need to declare the variable first before using it. Something like string characterClass = "Warrior"; CharClasses Class = new CharClasses(characterClass); :doh:

              Navaneeth How to use google | Ask smart questions

              1 Reply Last reply
              0
              • N nik121

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

                G Offline
                G Offline
                Gary Stafford
                wrote on last edited by
                #7

                If I understand your question, Leveling should inherit CharClasses and use the base constructor characterClass, 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 all public to protected, etc.

                Regards, Gary

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups