locked
Get and set? RRS feed

  • Question

  • Hey again. I've been trying out various bits of code but can't manage to make a working constructor, I need to implement get and set methods for variables in my text based game I am making for College, however I can only manage to "get" at the moment, and even then I can't use it in the main program, it only shows up in the class. I'm quite confused as I'm really new to this, thanks for taking the time to look through this!

    public class Item {
    	public int Potions = 0;
    	public int superPotions = 0;
    
        public Item(int getsuperPotions, int getPotions, int setPotion, int setsuperPotion)
        {
            getPotions = Potions;
            getsuperPotions = superPotions;
    
        }
    	public void setpotions(int apotions){
    	}
    	public void setsuperPotions(int asuperPotions){
    	}
    	public int getPotions(){
            return Potions;
    	}
    	public int getsuperPotions(){
        return superPotions;
    	}
    }

    this is the code for the "item" class, it has to be kept very basic. "potion" and "super potion" are just a placeholder for a number, the number the player has.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Dungeon_Crawler
    {
        class Program
        {
     
    
            static void Main(string[] args)
            {
    
            }
        }
    }

    This is the code for the main program, when i type getsuperpotion in, it doesn't recognize it as it has no connection to the other class, is there any way to fix this problem?

    All I need to be able to do is get and set the variables in the program, I just need to learn how this process is done correctly :P

    Thankyou!


    • Edited by Will_Barker Thursday, November 22, 2012 1:12 PM
    Thursday, November 22, 2012 1:02 PM

Answers

  • I think you are describing "properties" for your Item class.  I do not understand the constructor though.  "Get" usually implies your client code is reading a property, so it does not make sense to pass a value for get and set of properties in the constructor.  I think the class might be coded as follows:
     
    public class Item
    {
     public int Potions {get; set;}
     public int superPotions {get; set;}
     
     public Item (int initPotions, int initsuperPotions)
     {
      Potions = initPotions;
      superPotions = initsuperPotions;
     }
    }

    --
    Mike
    • Proposed as answer by Norkk Thursday, November 22, 2012 1:16 PM
    • Marked as answer by Jason Dot Wang Friday, November 30, 2012 7:38 AM
    Thursday, November 22, 2012 1:12 PM

All replies

  • I think you are describing "properties" for your Item class.  I do not understand the constructor though.  "Get" usually implies your client code is reading a property, so it does not make sense to pass a value for get and set of properties in the constructor.  I think the class might be coded as follows:
     
    public class Item
    {
     public int Potions {get; set;}
     public int superPotions {get; set;}
     
     public Item (int initPotions, int initsuperPotions)
     {
      Potions = initPotions;
      superPotions = initsuperPotions;
     }
    }

    --
    Mike
    • Proposed as answer by Norkk Thursday, November 22, 2012 1:16 PM
    • Marked as answer by Jason Dot Wang Friday, November 30, 2012 7:38 AM
    Thursday, November 22, 2012 1:12 PM
  • This works great cheers, how would I use the get and set in my main program code? So if my first code was to set potions to "5" how would I go about this?
    Thursday, November 22, 2012 1:22 PM
  • Like this:
     
     Item x = new Item(5, 10);
     console.WriteLine(x.Potions);

    --
    Mike
    Thursday, November 22, 2012 1:25 PM
  • As a followup, if you need to change the number of potions, then something like this:
     
        Item x = new Item(5, 10);
        Console.WriteLine(x.Potions);
        x.Potions = 7; // set it to 7...
        Console.WriteLine(x.Potions);

    --
    Mike
    Thursday, November 22, 2012 1:31 PM
  • Perfect!, thanks for your help I think I have this down now :) 

            static void Main(string[] args)
            {
    
                Items xitems = new Items();
                xitems.Potions = 2;
                xitems.superPotions = 1;
    
            }
        }
    }
    

    after doing this for all classes I think I could make a simple game, thank you!

    Thursday, November 22, 2012 1:39 PM
  • Just fyi, you could also use:
     
        Item x = new Item() {Potions = 2, superPotions = 1};

    --
    Mike
    Thursday, November 22, 2012 1:48 PM
  • woo!

    Thursday, November 22, 2012 2:03 PM