1
class bishop:unit {}
class knight:unit {}
class peasant:unit {}

void Battle(unit first, unit second, byte firstAmount, byte secondAmount)
{
  System.Array sideA = System.Array.CreateInstance(first.GetType(),firstAmount);
  for(int i=0; i< firstAmount; i++) 
  { 
   sideA[i] = ???
  }
}

In my last question I had problems with creating dynamic arrays and here is my next step problem! :D
Passable types to this method bishop, knight and etc
Actually I don't understand how to initialize objects now. I can't type just sideA[i] = new first.GetType()(constructor parameters) and understand why, but I don't understand how to work around this

5
  • 1
    Are you sure that your code is in C#? Classes with the first letter small and alone-standing function ... Commented Sep 3, 2013 at 7:18
  • 1
    Which programming language are you using?? Commented Sep 3, 2013 at 7:18
  • 1
    @Kyle C# with bad naming style, look at System.Array.CreateInstance Commented Sep 3, 2013 at 7:19
  • Are you using polymorphism? You should make a base interface called, lets say figure (according to other classes named by chess figures) and then make other classes implement this interface. This allows you to pass any figure as parameter. Commented Sep 3, 2013 at 7:20
  • 1
    Thanks for critism, I will definitely study C# naming convention! And this piece of code just a mockup of my case Commented Sep 3, 2013 at 7:29

2 Answers 2

4

This is really, really bad design.

I assume, that your method Battle could be an instance method of a class Game, which you didn't provide to us.

Then I strongly recommend, that the Battle method should NOT create instances of objects it works with. It should only take them and do a battle action (compute lives, etc.).

So, create these objects elsewhere and then just post them to the method.

class Game
{
    List<Bishop> bishops = new List<Bishop>() { new Bishop(..), ... };
    List<Knight> knights = new List<Knight>() { new Knight(..), ... };

    void Battle(List<Unit> first, List<Unit> second)
    {
        foreach(var f in first)
        {
            // get random unit from the second collection and attack him
            f.Attack(GetRandomKnight(second)); 
        }
    }

    public void StartBattle()
    {
        Battle(bishop, knights);
    }
}

Also make sure that you use correct C# naming. A name of a class should start with a capital letter.

class Unit
{
    public virtual void Attack(Unit enemy)
    {
        // default attack
        Kick(enemy);
    }

    protected Kick(Unit enemy) { ... }
}

class Bishop : Unit { }
Sign up to request clarification or add additional context in comments.

Comments

2

Ondrej has a good answer. Just to help you with arrays, you should never use reflection unless with a good reason. I see no reason why you should be doing it here. You can use the typical new keyword to instantiate an array.

void Battle(unit first, unit second, byte firstAmount, byte secondAmount)
{
  var sideA = new unit[firstAmount];
  for(int i=0; i< sideA.Length; i++) 
  { 
    sideA[i] = ???
  }
}

If the instantiated array should really be of run time type of first, then you can rely on generics.

void Battle<T1, T2>(T1 first, T2 second, byte firstAmount, byte secondAmount) 
                   where T1 : unit where T2 : unit
{
  var sideA = new T1[firstAmount];
  for(int i=0; i< sideA.Length; i++) 
  { 
    sideA[i] = ???
  }
}

The totally dynamic way to settle things will be SetValue and GetValue:

void Battle(unit first, unit second, byte firstAmount, byte secondAmount)
{
  var sideA = Array.CreateInstance(first.GetType(),firstAmount);
  for(int i=0; i< firstAmount; i++) 
  { 
   sideA.SetValue(???, i);
   sideA.GetValue(i); //to get the value back.
  }
}

Basically you dont get indexer syntax for System.Array.

1 Comment

+1 for generics sample ... also thank you for the revision of my post

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.