0

I have the following method where I get an integer from the user, it's for a small text based game I'm making, I'm using OO though.

public int getInt(string information)
        {
            int num = 0;

            try
            {
                Console.WriteLine("What is your " + information + "?: ");
                num = int.Parse(Console.ReadLine());
                Console.WriteLine("Your " + information + " is: " + num);
            }
            catch
            {
                Console.WriteLine("Value: '" + num + "' was not a numerical character!");
            }

            setStats(information, num);

            return num;
        }

This works as intended.

However, my setStats method doesn't work as intended.

public void setStats(string nameofStat, int valueofStat)
        {

            PropertyInfo prop = stat.GetType().GetProperty(nameofStat);

            prop.SetValue(nameofStat, valueofStat, null);

            Console.WriteLine(stat.ToString());
        }

I'm using the reflection class because I don't want to do this for every new stat.

                if (nameofStat == "Agility")
                {
                   stat.agility = 1;
                }

The error I get is "Object reference not set to an instance of an object." because my prop instance is null, but I don't know why it's null.

on the line:

prop.SetValue(nameofStat, valueofStat, null);

Please help!

Here is an example of one of my get;set; properties in my Stats class.

private int Agility;

     public int agility
            {
                get { return Agility; }
                set { Agility = value;}
            }
mikus andi
  • 65
  • 1
  • 1
  • 9
  • Debug this code and see if you're actually getting a PropertyInfo. From first glance I'm noticing an "Agility" and "agility", so I'm wondering if it's not getting the property because of a case sensitive issue – oppassum Jul 06 '15 at 15:10
  • my "prop" is currently null, but I don't know why it's null. and where are you noticing the Agility issue? I can't see it? – mikus andi Jul 06 '15 at 15:11
  • Make sure your variable is public, make sure the case is correct. As a debug test you can output each of the properties to the console or just step through after doing this: System.Reflection.PropertyInfo[] properties = stat.GetType().GetProperties(); foreach (System.Reflection.PropertyInfo info in properties) { Console.WriteLine(info.Name); } – oppassum Jul 06 '15 at 15:15
  • Could my get;set; property be the issue? the private variable is "Agility" and the get;set; is "agility" – mikus andi Jul 06 '15 at 15:17
  • where is stat defined? – sertsedat Jul 06 '15 at 15:17
  • What are you putting for nameOfStat? "Agility" or "agility". Also c# coding standards have the uppercase character public and lowercase private. – oppassum Jul 06 '15 at 15:20
  • nameOfstat is always a single Uppercase character followed by lowercase characters inp.getInt("Agility"); inp.getInt("Dexterity"); inp.getInt("Strength"); – mikus andi Jul 06 '15 at 15:21
  • I don't know what uppercase followed by lowercase means. Regardless, you need nameOfStat to be the name of the property (the get;set;), not the private variable. I suspect that's your issue – oppassum Jul 06 '15 at 15:22
  • I changed my code a little, and now I'm getting the error "Object does not match target type." prop is no longer null. – mikus andi Jul 06 '15 at 15:24
  • FIXED IT. yeah, that was partly the issue, I changed my code. It now works as intended. – mikus andi Jul 06 '15 at 15:27

0 Answers0