0

I am creating a console zombie survival game that is suppose be random and be fairly complex, such as finding multiple weapons, and ammo types. However, I believe that I could save an object in array to use it again later, for correct information, but this way does not work.

I have tried looking of storing objects in arrays with no luck. I have tried to change the arrays from Item based to Object based with no luck.

class Firearm : Weapon
    {
        public Firemode firemode;
        public int maxCapacity;
        public int curCapacity;
        public Type type;
        public Caliber caliber;

        public Firearm (string _name, float _damage, int _range, Firemode 
_firemode, int _maxCapacity, int _curCapacity, Type _type, Caliber 
_caliber)
        {
            name = _name;
            damage = _damage;
            range = _range;
            firemode = _firemode;
            maxCapacity = _maxCapacity;
            curCapacity = _curCapacity;
            type = _type;
            caliber = _caliber;
        }

        public void FirearmStats()
        {
            Console.WriteLine("\n Name: {0}\n Damage: {1}\n Range: {2}\n 
Firemode: {3}\n Max Capacity: {4}\n Current Capacity: {5}", name, damage, 
range, firemode, maxCapacity, curCapacity);
        }
    }


static void Main(string[] args)
    {
        Random numberGenerator = new Random();
        //Pistols
        Firearm m9 = new Firearm("Beretta M9A1", 34.0F, 30, 
Firemode.SemiAutomatic, 15, numberGenerator.Next(1, 16), Type.Pistol, 
Caliber.nine);
        ....
        Item[] inventory = new Item[14];
        Item[] equipped = new Item[0];
        .....
        equipped[0] = m9;
        Console.WriteLine("\nYou find a {0}", equipped[0].name);

        //issue here with equipped[0]^^^
        equipped[0].FirearmStats();

        //and here ^^^^^
        player.Command();
    }

Severity Code Description Project File Line Suppression State Error CS1061 'Program.Item' does not contain a definition for 'FirearmStats' and no accessible extension method 'FirearmStats' accepting a first argument of type 'Program.Item' could be found (are you missing a using directive or an assembly reference?) ZombieConsoleGame E:\Visual Studio Projects\ZombieConsoleGame\ZombieConsoleGame\Program.cs 158 N/A

1 Answer 1

1

I am assuming the Weapon class inherits from your Item class.

Because the array equipped is of type Item, you need to cast the Item to a Weapon before you can call any of the methods/functions on the Weapon class.

Alter this line (where you highlighted the problem) equipped[0].FirearmStats();

to be:

((Firearm)equipped[0]).FirearmStats();

This will cast the Item to be a Firearm and then allow you to use it as a Firearm.

Edit:

In addition, the line:

Item[] equipped = new Item[0];

Will create an Array variable with Zero elements within it, no space has been allocated to store the items you are equipped with. Change the zero in [0] to be a positive number like you already have in the previous line for inventory (Item[] inventory = new Item[14];)

Sign up to request clarification or add additional context in comments.

8 Comments

Side note/nitpick: It is not Weapon but FireArm which declares the FirearmStats method ;-)
I thought this would work after seeing it, but it did not. Then I thought I should change it to the cast as Firearm instead. This got rid of any errors till I ran it. In the console it says "Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at ZombieConsoleGame.Program.Main(String[] args) in E:\Visual Studio Projects\ZombieConsoleGame\ZombieConsoleGame\Program.cs:line 157". I also tried changing the array to be of Weapon or Firearm but got the same error message in the console when running it.
@SubaruDanyBoon, pay attention to what the IndexOutOfRangeException tells you. Don't just blind-guess the problem, read the problem description given to you by the exception...
I have no clue why its saying that, should it not be within range, because the index is within range of the array size.
@SubaruDanyBoon, what is the actual array size?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.