c# - Dynamically Create Object and Add to List -
i use method create instance of adventurer
, add list<adventurer>
. loop create 20 instances of adventurer
. trying avoid using constructor since down road have quite few values pass in. if trying not possible, have revert using constructor.
i need method create new instance of adventurer
, assign values randomgender
,randomclass
, , name
it. @ point don't know how. also, can't create new instance , manually name since method 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 loop:
(int = 0; < 20; i++) { adventurermanager.avaliableadvlist.add(adventurermanager.createadventurer()); }
so question is... in createadventurer
possible assign values new instance of adventurer
without manually declaring , without use of constructor? or easier have long constructor somewhere down road?
you can create new object , assign values directly that.
public static adventurer createadventurer() { adventurer = 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 return player random gender, random class, , name of... test 1-20.
Comments
Post a Comment