dimanche 23 juillet 2017

Creating an object of a random sub class in C#

I'm pretty new to C# so please excuse my beginners question. I have allready searched but haven't found the solution I wanted.

I have an abstract class and several sub classes of it. Let's just say subclass1 to subclass9.

What I want to do now is to generate an object from a random subclass. Actually I'd like to have some lists including a preselection of those classes. Like: List1 including sub classes 1, 3, 4 and 6, List2 including 1, 2, 5, 6 and 9 and so on. So I can say anytime: Give me a random object from List2.

My idea was to generate arrays and putting those subclasses into them, then selecting a random element of the array. But that didn't work out for me. I haven't figured out how to fill the arrays with objects, so I am not sure if this isn't possible or I just haven't found the right syntax. Can anyone give me a hind on how to generate those lists?

Since I am allready asking: Is there an easy way to give the objects in the lists different probabilities to occour?

Thank you for any help!




Aucun commentaire:

Enregistrer un commentaire