2

I'm trying to create some classes for a small windows game in C#. I have a class Weapon with a constructor, which inherits from my base class Item:

public class Weapon : Item
{
    public int Attack { get; set; }

    //constructor
    public Weapon(int id, string name, int value, int lvl, int attack) : 
        base(id, name, value, lvl)
    {
        Attack = attack;
    }
}

Item class:

public class Item

{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Value { get; set; }
    public int Lvl { get; set; }

    //constructor
    public Item(int id, string name, int value, int lvl)
    {
        ID = id;
        Name = name;
        Value = value;
        Lvl = lvl;
    }

}

This all works fine, I can call my constructor and create instances of Weapon object. However, I also want my Item and Weapon classes to inherit from the PictureBox class, like this:

public class Item : PictureBox 

{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Value { get; set; }
    public int Lvl { get; set; }

    //constructor
    public Item(int id, string name, int value, int lvl)
    {
        ID = id;
        Name = name;
        Value = value;
        Lvl = lvl;
    }

}

However applying the code above leads to an error "Constructor on type 'MyNamespace.Item' not found"

Am I approaching this the correct way? How can I get my Item base class to inherit from another base class?

EDIT: the error comes from within VS when opening the class file: enter image description here

Why is it trying to open my class file in the form designer? I don't understand!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
pvdev
  • 230
  • 3
  • 13
  • 1
    I strongly suggest using a `Bitmap` attribute and not override the `PictureBox` – kevintjuh93 Oct 15 '15 at 09:57
  • 1
    A weapon is not a substitute for a PictureBox. I think you should probably rethink your design. – Darren Young Oct 15 '15 at 10:02
  • Picturebox has all the properties I need. I only have a day to create this so the overall design is not important as long as it works. I will take a look at the Bitmap attibute though. Thanks – pvdev Oct 15 '15 at 10:06
  • 1
    I assume you're just working with picture boxes here, so it's really an ItemPictureBox, WeaponPictureBox etc. – Jeb Oct 15 '15 at 10:12

2 Answers2

2

I believe it is correct as long as the base class is not marked as sealed and has an appropriate constructor. When you create the Item class, the constructor will now need to call the base PictureBox constructor, and use one of its public constructors.

e.g.:

//constructor
public Item(int id, string name, int value, int lvl)
: base(...params etc)
{
Jeb
  • 3,689
  • 5
  • 28
  • 45
  • 1
    Nitpicking here, but you only need to call the base class explicitly if the constructor has parameters. Parameterless constructors are implied. – James Oct 15 '15 at 10:01
  • I think the PictureBox class has a parameterless constructor - can I use ": base()" with no parameters? – pvdev Oct 15 '15 at 10:02
  • James has the correct answer here; if it is parameterless (i.e. the Picture Box contains a default constructor), then you don't need to do this at all. – Jeb Oct 15 '15 at 10:03
  • I'm getting the forementioned error though, so something must be missing? – pvdev Oct 15 '15 at 10:08
  • Not sure to be honest. You will need to ensure you have a reference to the assembly containing the System.Windows.Forms.PictureBox type in your project. However, the compiler would have told you that... – Jeb Oct 15 '15 at 10:10
  • Yep dll is referenced. Funny thing is that the code works fine, i'm able to create a Weapon instance and access PictureBox properties. But it's visual studio which kicks off an error when I try and open the cs file. – pvdev Oct 15 '15 at 10:13
  • Ah, you might need a constructor without parameters which calls the InitializeComponent() method...- see http://stackoverflow.com/questions/1216940/net-inherited-winforms-form-vs-designer-issue perhaps helps? – Jeb Oct 15 '15 at 10:28
1

You need a parameterless constructor; otherwise, the designer cannot display your control (since you derive it from PictureBox, it is a control now, so opening the file by double click will load the designer).

According to the component-based approach, the components must be able to be restored by creating them by a default constructor and by setting the public properties, which you can set in a property grid.

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • Not quite sure I understand. Do you mean I need a parameterless constructor for my Item class? I want to be able to create an instance of Item using a constructor by passing in properties, are you saying this can't be done if i'm inheriting from a control? – pvdev Oct 15 '15 at 11:06
  • For the `Item` class and for its derived classes, yes. Without a parameterless constructor, you will not able to open them in the designer or place them on a `Form` like a regular `PictureBox`. Just check any of the controls, they all have only a parameterless constructor and you can configure them purely by their properties. Without a parameterless constructor you will have to create your `Item`s and place them in a parent `Form`, `Panel` or whatever programmatically. – György Kőszeg Oct 15 '15 at 20:01
  • Ah okay, I understand now. I'm only working with these objects programmatically so that shouldn't be an issue. Many thanks. – pvdev Oct 16 '15 at 07:55