-1

I'm learning to code. I have started a little project where I design a text-based RPG. I am struggling with storing and retrieving objects in and from an array. Please have a look at my progress so far and tell me what I am doing wrong. If I am using a wrong approach please also let me know how to do the whole thing smarter :)

First I define some properties of the player:

static class Globals
{
    public static string playername;
    ...
    public static object[] playerInventory = new object[4];
}

Then I create the weapon class:

public class Weapon
{
    public string weaponName;
    public int weaponBaseDamage;
    

    public Weapon(string name, int baseDamage)
    {
        weaponName = name;
        weaponBaseDamage = baseDamage;
    }

Then I create the first basic weapon and try to store it in an array.

public class Program
{
    public static void Main()
    {
        Weapon StartSword = new Weapon("My first Sword", 1);
        Globals.playerInventory[0] = StartSword;
        Console.WriteLine(StartSword.weaponName); // This works
        Console.WriteLine(Globals.playerInventory[0]); // This prints "CSharp_Shell.Weapon", but I expected "StartSword"
        Console.WriteLine(Globals.playerInventory[0].weaponName); // This results in an Error

The unexpected result of the second WriteLine command tells me that something must be quite wrong, but I don't know what it is and how to fix it. Any advice is welcome! (And please keep in mind that I am new to Coding).

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Max
  • 53
  • 4
  • Are all the inventory items weapons? – Mureinik Jan 05 '21 at 22:28
  • 1
    Object has no property/field called “weaponName” so it is not type-valid. Use a generic collection (with an interface if required) and/or the as/is operators. The first WriteLine works because it accepts an Object-typed expression, and all objects have a ToString() method. See https://learn.microsoft.com/en-us/dotnet/standard/generics/collections – user2864740 Jan 05 '21 at 22:30
  • 1
    When you print an object, it will print out the type of the object, in your case, the class which is `Weapon` plus a prefix. Referring to `Console.WriteLine(Globals.playerInventory[0]);` – Nathaniel Cutajar Jan 05 '21 at 22:35
  • You must cast your Globals.playerInventory[0] before to access its property – Joe Taras Jan 05 '21 at 22:35

2 Answers2

0

It's required Typecasting. Please try like below:

Console.WriteLine(((Weapon)Globals.playerInventory[0]).weaponName)
-1

Ok, lets look at what your code does:

Weapon StartSword = new Weapon("My first Sword", 1);
Globals.playerInventory[0] = StartSword;
Console.WriteLine(StartSword.weaponName); // This works

Above you create an object of the type Weapon with the name "My first Sword". And then print the name of that public property that is populated in the constructor.

  Console.WriteLine(Globals.playerInventory[0]); // This prints "CSharp_Shell.Weapon", but I expected "StartSword"

Here you try to write an object. But an object is not a string so c# will automatically try to convert that to a string and will look at the type. So it is expected that it will not write the properties but a representation of the type.

Console.WriteLine(Globals.playerInventory[0].weaponName); // This results in an Error

Globals.playerInventory is defined as object[] playerInventory, so even if we know that you have entered an object of type weapon there, we need to specify that. Either by letting playerInventory be of the type Weapon[] playerInventory, or by type casting your object before using its properties, like this:

Console.WriteLine(((Weapon)Globals.playerInventory[0]).weaponName);
Achtung
  • 682
  • 3
  • 16