1

I am creating a game in C# Windows form and I will have 9 different characters inside the game for my player to choose from (using radio buttons). I have a variable called PlayerChar and Whenever the player chooses a character I will store the character's name inside that variable. Also I have 9 different classes for each characters. What I am trying to do is, to get the program to create an object from the class of the character chosen. but I don't know how to use variable PlayerChar as my class name.

PlayerChar obj = new PlayerChar();

This is basically what I am trying to do. Also I tried using Activator.CreateInstance:

PlayerChar myObj = Activator.CreateInstance(PlayerChar);

4 Answers 4

2

You are fixating on the wrong way to solve the problem. You don't need to create the player from a variable that is a class name; the variable can be anything, and you just need to match it to a method that constructs and object of the correct class. This is done using some kind of factory.

For example, here's a mini-factory that creates Player instances from strings:

Player CreatePlayerInstance(string type)
{
    var creatorMap = new Dictionary<string, Func<Player>>() {
        { "foo", () => new FooPlayer() },
        { "bar", () => new BarPlayer() },
    };

    return creatorMap[type]();
}

Obviously there is no error checking or anything, but this is the core idea. You can extend and modify it in many many ways, depending on your goals.

Sign up to request clarification or add additional context in comments.

Comments

1

Have PlayerChar be the base class for all of your character classes.

public class PlayerChar
{
    ...
}

public class WarriorChar : PlayerChar
{
    ...
}

public class RangerChar : PlayerChar
{
    ...
}

public class ClericChar : PlayerChar
{
    ...
}

Then use a switch block or sequential if blocks to instantiate your player object with the right character subclass.

PlayerChar myObj = null;

if (warriorButton.IsSelected)
    myObj = new WarriorChar();
else if (rangerButtom.IsChecked)
    myObj = new RangerChar();
else if (clericButton.IsChecked)
    myObj = new ClericChar();
...

(Don't use the Activator class to instantiate your objects unless you fully understand what Activator does and have a well-thought-out reason for using it. Nine times out of ten it is used by people trying to get fancy with their code and end up just overcomplicating things.)

3 Comments

@pinkfloydx33 The example in that blog I see as a bad example. It presents the problem as some kind of great design-breaking issue, but that issue is easily solved with an abstract method bool EquipWeapon(Weapon weapon) that checks if the weapon can be equipped before it is assigned to the Weapon field.
I'm not sure about your last statement. A lot of code uses activator (likely without the developer realizing it) Generic classes with a new() constraint followed by new T() is replaced by the compiler with a call to Activator.CreateInstance<T>. See remarks section here msdn.microsoft.com/en-us/library/0hcyx2kd(v=vs.110).aspx
@pinkfloydx33 That would be an example of it being implemented by people who know what they are doing. The counterexample would be someone using Activator.CreateInstace to instantiate a variable just because for some reason they couldn't get the new keyword to work.
1

Try this

var typeName = $"MyNamespace.{playerCharacter}";
var type = Type.GetType(typeName);
var instance = Activator.CreateInstance(type);

Comments

-2

Option 1

Create two global variables to store your information. Then use if statements to assign the different characters and classes.

Option 2

Create a bunch of methods to manipulate and store data with global variables in form.

Option 3

Create two C# class files one for your character and the other for your class.

Create two C# class files with methods and calling them from main program or where they are needed.

Tutorial for methods: https://msdn.microsoft.com/en-us/library/ms173114.aspx

Tutorial for classes: https://msdn.microsoft.com/en-us/library/x9afc042.aspx

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.