0

In my program, I have created a class called Monsters. It is defined by four different enums: Species, Rarity, Region and Type. I am trying to create a list. Here is the code I have written:

public class Monsters

{
    public Enums.Species monsterName;
    public Enums.Rarity rarity;
    public Enums.Region region;
    public Enums.Type monsterType;

    public Monsters(Enums.Species monsterName, Enums.Rarity rarity, Enums.Region region, Enums.Type monsterType)
    {
        this.monsterName = monsterName;
        this.rarity = rarity;
        this.region = region;
        this.monsterType = monsterType;
    }

    public Enums.Species getmonsterName()
    {
        return monsterName;
    }

    public Enums.Rarity getrarity()
    {
        return rarity;
    }

    public Enums.Region getregion()
    {
        return region;
    }

    public Enums.Type getmonsterType()
    {
        return monsterType;
    }

    List<Monsters> startersPokemon = new List<Monsters>();
    startersPokemon.Add(Enums.Species.PIKACHU, Enums.Rarity.rare, Enums.Region.Pallet, Enums.Type.electric);

}

When I type the final line of code where I try to add to the list, I keep getting an error telling me it is a field being used as a type. How can I rectify this problem? If a list isn't the answer to my problem, would an array be better?

5
  • 1
    Btw.. You should use properties instead of 'get*' methods. Also, its bad practice to name something 'Type' when not referring to an actual class Commented Jun 16, 2014 at 1:27
  • It's also a good practice to use the PascalCase formatting for method names (i.e. GetRarity instead of getrarity) Commented Jun 16, 2014 at 2:02
  • @RobertLevy Can you explain the difference of properties and get methods? Commented Jun 16, 2014 at 13:23
  • @Pierre-LucPineault Thank you for the reminder. I usually do go with the PascalCase formatting but must have slipped my mind on this one. Made that change already. Commented Jun 16, 2014 at 13:28
  • @R1zzo23 - see msdn.microsoft.com/en-us/library/… Commented Jun 16, 2014 at 13:33

2 Answers 2

1

I found 2 issues in your code.

1.startersPokemon shouldn't be declared in the Monsters class. It can be a class:

public class MonstersList
{
    List<Monsters> startersPokemon = new List<Monsters>();
    // init startersPokemon 
    public MonstersList()
    {
        // please read 2nd point.
        startersPokemon.Add(...);
    }
}

2.You should add an instance of Monsters to your list. You can change your code to :

startersPokemon.Add(
    new Monsters
    (
        Enums.Species.PIKACHU, 
        Enums.Rarity.rare, 
        Enums.Region.Pallet, 
        Enums.Type.electric
    )
);
Sign up to request clarification or add additional context in comments.

1 Comment

I understand what you mean, but the problem is when I type startersPokemon.Add, I am getting the error "startersPokemon is a 'field' but used like a 'type' ". Do you have any pointers on how to remedy that situation?
0

Your code is just sitting there in the middle of the class definition. It needs to be inside a method which would be invoked from somewhere. That method could be your Monsters constructor that you already have or you could create a seperate method.

public void Initialize() 
{
    List<Monsters> startersPokemon = new List<Monsters>();  
    startersPokemon.Add(
    new Monsters
    (
        Enums.Species.PIKACHU, 
        Enums.Rarity.rare, 
        Enums.Region.Pallet, 
        Enums.Type.electric
    )
    );
}

Comments

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.