Jason D
Jason D

Reputation: 2664

Dynamically Create Object and Add to List

I would like to use a method to create an instance of Adventurer and then add it to a List<Adventurer>. A for loop will create 20 instances of Adventurer. I am trying to avoid using a constructor since down the road I will have quite a few values to pass in. If what I am trying to do is not possible, I will have to revert to using the constructor.

I need this method to create a new instance of Adventurer and assign values randomGender,randomClass, and name to it. At this point I don't know how. Also, I can't create a new instance and manually name it since this method will be called many times.

    public static Adventurer createAdventurer()
    {
        Array valuesGender = Enum.GetValues(typeof(AdventurerGender));
        AdventurerGender randomGender = (AdventurerGender)valuesGender.GetValue(rand.Next(valuesGender.Length));

        Array valuesClass = Enum.GetValues(typeof(AdventurerClass));
        AdventurerClass randomClass = (AdventurerClass)valuesClass.GetValue(rand.Next(valuesClass.Length));

        string name = "Test " + AdventurerManager.AvaliableAdvList.Count.ToString();

        return new Adventurer();
    }

Here is my for loop:

        for (int i = 0; i < 20; i++)
        {
            AdventurerManager.AvaliableAdvList.Add(AdventurerManager.createAdventurer());
        }

So my question is... in createAdventurer is it possible to assign values to a new instance of Adventurer without manually declaring it and without use of a constructor? Or would it be easier to just have a very long constructor somewhere down the road?

Upvotes: 2

Views: 1093

Answers (2)

Seist
Seist

Reputation: 42

You can create a new object and assign the values directly to that.

public static Adventurer createAdventurer()
{
    Adventurer a = new Adventurer;        

    Array valuesGender = Enum.GetValues(typeof(AdventurerGender));
    AdventurerGender randomGender = (AdventurerGender)valuesGender.GetValue(rand.Next(valuesGender.Length));

    Array valuesClass = Enum.GetValues(typeof(AdventurerClass));
    AdventurerClass randomClass = (AdventurerClass)valuesClass.GetValue(rand.Next(valuesClass.Length));

    string name = "Test " + AdventurerManager.AvaliableAdvList.Count.ToString();

    a.Gender = randomGender;
    a.Class = randomClass;
    a.Name = name;

    return a;
}

This method will return a player with a random gender, random class, and name of... Test 1-20.

Upvotes: 2

McGarnagle
McGarnagle

Reputation: 102723

It looks like you're after a private constructor. Do all the assigning there, and call the private constructor from your static factory method.

public static Adventurer createAdventurer()
{
    return new Adventurer();
}

private Adventurer()
{
    Array valuesGender = Enum.GetValues(typeof(AdventurerGender));
    AdventurerGender randomGender = (AdventurerGender)valuesGender.GetValue(rand.Next(valuesGender.Length));

    Array valuesClass = Enum.GetValues(typeof(AdventurerClass));
    AdventurerClass randomClass = (AdventurerClass)valuesClass.GetValue(rand.Next(valuesClass.Length));

    string name = "Test " + AdventurerManager.AvaliableAdvList.Count.ToString();

    // TODO assign member values here
}

Upvotes: 3

Related Questions