Enums & Constructors?

C

CSharp-Jay

So I am creating a playful little game engine. And in my BaseItem
class I have this:

protected string name; // Item name
protected enum itemType { Armor, Helmet, Shield, Sword }; //
Item type
protected int value; // Item value (Attack Power or Defense)

But I can't for the life of me figure out how I would do the
overloaded constructor using an enum? I might not be doing it right
since I don't know much about enums. But in this case an enum seems
appropriate to differentiate armor from weapons you know? Please let
me know!
 
P

Peter Duniho

CSharp-Jay said:
So I am creating a playful little game engine. And in my BaseItem
class I have this:

protected string name; // Item name
protected enum itemType { Armor, Helmet, Shield, Sword }; //
Item type
protected int value; // Item value (Attack Power or Defense)

But I can't for the life of me figure out how I would do the
overloaded constructor using an enum? I might not be doing it right
since I don't know much about enums. But in this case an enum seems
appropriate to differentiate armor from weapons you know? Please let
me know!

An enum is just a type. You use the enum name like you'd use any other
type. So, just make a constructor parameter of type "itemType" for any
constructor where you want to be able to pass a value of the enum type.

Pete
 
C

CSharp-Jay

An enum is just a type.  You use the enum name like you'd use any other
type.  So, just make a constructor parameter of type "itemType" for any
constructor where you want to be able to pass a value of the enum type.

Pete

Still getting weird errors. Here is what I have for my code so far,
getting the error:Error 1 Inconsistent accessibility: parameter type
'GameEngine.BaseItem.ItemTypes' is less accessible than method
'GameEngine.BaseItem.BaseItem(string, GameEngine.BaseItem.ItemTypes,
int)'

class BaseItem
{
protected string name; // Item name
protected enum ItemTypes { Armor, Helmet, Shield, Sword,
Potion, Quest_Item }; // Item type
protected ItemTypes itemType;
protected int _value; // Item value (Attack Power or Defense)

public BaseItem(string name, ItemTypes itemType, int _value)
{
this.name = name;
this.itemType = itemType;
this._value = _value;
}

~BaseItem() { }
}
 
C

CSharp-Jay

So I am creating a playful little game engine.  And in my BaseItem
class I have this:

        protected string name; // Item name
        protected enum itemType { Armor, Helmet, Shield, Sword };//
Item type
        protected int value; // Item value (Attack Power or Defense)

But I can't for the life of me figure out how I would do the
overloaded constructor using an enum?  I might not be doing it right
since I don't know much about enums.  But in this case an enum seems
appropriate to differentiate armor from weapons you know?  Please let
me know!

Hmm, okay well I used your advice to fix it up a bit but now I am
getting this error: Error 1 Inconsistent accessibility: parameter type
'GameEngine.BaseItem.ItemTypes' is less accessible than method
'GameEngine.BaseItem.BaseItem(string, GameEngine.BaseItem.ItemTypes,
int)' C:\Users\BlueSin\Documents\Visual Studio 2008\Projects\GameEngine
\GameEngine\BaseItem.cs 15 16 GameEngine

Here is the code so far, and sorry if this turns into a double post,
my original follow up post yours was nuked somehow.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameEngine
{
class BaseItem
{
protected string name; // Item name
protected enum ItemTypes { Armor, Helmet, Shield, Sword,
Potion, Quest_Item }; // Item type
protected ItemTypes itemType;
protected int _value; // Item value (Attack Power or Defense)

public BaseItem(string name, ItemTypes itemType, int _value)
{
this.name = name;
this.itemType = itemType;
this._value = _value;
}

~BaseItem() { }
}
}
 
C

CSharp-Jay

An enum is just a type.  You use the enum name like you'd use any other
type.  So, just make a constructor parameter of type "itemType" for any
constructor where you want to be able to pass a value of the enum type.

Pete

Okay I got it, thanks again Pete!

protected string name; // Item name
public enum ItemTypes { Armor, Helmet, Shield, Sword, Potion,
Quest_Item }; // Item type
protected ItemTypes itemType;
protected int _value; // Item value (Attack Power or Defense)

public BaseItem(string name, ItemTypes itemType, int _value)
{
this.name = name;
this.itemType = itemType;
this._value = _value;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top