-1

Here is the case I have got a spell class

    public abstract class Spell
{

    public abstract void Castspell ();
}

public class Heal:Spell
{
    Dictionary<string, int> heals = new Dictionary<string,int> ();

    public Heal()
    {
        heals.Add ("less heal", 1);
        heals.Add ("medium heal", 2);
        heals.Add (" large heal", 3);
    }

    public override void Castspell()
    {
    }


}

ignore the Castspell(), and there is another class called student

    public class student
{
  public enum spells
    {
        lessheal,
        mediumheal,
        highheal
    }
    List<Spell> _skillist = new List<Spell>();
    public student()
    {
           //...
    }
            public void learnskills()
    {
        _skillist.Clear ();
        Spell newspell = new Spell ();
        _skillist.Add (newspell);

    }

what I am trying to do here is I wanna make learn skill method, each time I Call It will randomly add a heal spell into the _skillist.(it could be less, medium or high). how could I achieve it? please give me some advices about it.thanks

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35

1 Answers1

1

This question is extremely broad ... however I'd suggest using a combination of the following:

  • Writing a method on "Heal" to create an instance from a spells enum value
  • Using the System.Random class to generate a random number
  • Using this answer to get a random item from the spells enum using that random number

However, in order to do this, you're going to need to change that code a bit. Since you're not asking about a specific issue with your code sample, I can't do much more without rewriting it all for you, so I'll leave that part up to you!

Edits:

  • Use either a string Dictionary or an enum to refer to your spells, but pick one and be consistent. You're using both - a string Dictionary (e.g. "medium heal") in one class but enums (spells.mediumheal) in another. I'd suggest the enum if all you need is a list of spells with associated numbers.
  • Regardless of what you're using, it needs to be accessible from both classes. If you want to use an enum, declare it in the namespace, not in a class; if you really like a Dictionary , perhaps put it in a separate public static class as in this example.
  • If want to use an enum, this answer might give you some ideas
  • In general, the code could really benefit from some clean-up, but that's outside of the scope of your question
Community
  • 1
  • 1
Matt Wanchap
  • 841
  • 8
  • 20
  • It's certainly not wrong, but that "heals" dictionary isn't public, so it's not going to be usable from a learnskills() method in the student class unless you change the access modifier. – Matt Wanchap May 22 '16 at 06:02
  • I understand the first two points you said, but how do I actually indicate the it from the heal class? I mean I make a random class to generate a number for example it's two which return the "medium heal" but how could I make it relate to the heals dictionary ? I'm really confused about this part – Yunxiang Li May 22 '16 at 06:30
  • I'll update my answer rather than doing our whole discussion in the comments – Matt Wanchap May 22 '16 at 22:56
  • Also, please mark my answer as the answer to your question if I've helped you out – Matt Wanchap May 22 '16 at 23:04