4

Deck クラスで ToString() メソッドを呼び出そうとすると、「NullReferenceException was unhandled」というエラーが表示されます。いくつかのテストを行ったところ、Deck.ToString() メソッドを呼び出そうとすると、サイズが 0 に設定されていることがわかりました。コンストラクターが呼び出されたときに設定されるはずですが、何がリセットされているのかわかりません。それ。

主な方法:

public class DeckTest
{

    public static void Main()
    {
        Deck toTest = null;
        Console.WriteLine("What type of Deck would you like to create?");
        toTest = new Deck(Console.ReadLine());

        Console.WriteLine(toTest);

    }

}

デッキ クラス:

class Deck
{
    String type;
    int size;
    String deckList;
    Card[] deck;

    public Deck(String type, int size)
    {
        deck = new Card[size];
        this.type = type;
        this.size = size;

       while (deck[size - 1] == null)
       {

           Card newCard;
           Console.WriteLine("Please Enter the Type, Name, Colour (if card has no Colour, enter 'Colourless'), Colour Identity, Text, Mana Cost (if applicable), Converted Mana Cost (if applicable), Power (if applicable), Toughness (if applicable), separated by semicolons.");
           String line = Console.ReadLine();
           string[] card = line.Split(';');
           if (card[0].Equals("Land", StringComparison.OrdinalIgnoreCase))
           {
               newCard = new Card(card[0], card[1], card[3], card[4]);
           }
           else if (card[0].Equals("creature", StringComparison.OrdinalIgnoreCase))
           {
               newCard = new Card(card[0], card[1], card[2], card[3], card[4], card[5], int.Parse(card[6]), int.Parse(card[7]), int.Parse(card[8]));
           }
           else
           {
               newCard = new Card(card[0], card[1], card[2], card[3], card[4], card[5], int.Parse(card[5]));
           }
           addCard(newCard);
           }
   }


    public Deck(String type)
    {
        if (type.Equals("Standard", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 60);
        }
        else if (type.Equals("Extended", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 60);
        }
        else if (type.Equals("Modern", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 60);
        }
        else if (type.Equals("Commander", StringComparison.OrdinalIgnoreCase)|| type.Equals("EDH", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 100);
        }
    }

    void addCard (Card newCard)
    {
        int count = 0;

        while (deck[count] != null && count < size)
        {
            count++;
        }

        if (count < size)
        {
            deck[count] = newCard;
        }
        else
        {
            Console.WriteLine("This deck is full.");
        }
    }

    public override string ToString()
    {
        String output = "";
        int count = 0;

        while (deck[count] != null && count < size-1)
        {
            output += deck[count] + "/n";
        }

        return output;
    }

}
4

3 に答える 3

3

newキーワードは、コンストラクターから既存のインスタンスを変更するのではなく、まったく新しいインスタンス(すぐに破棄される)を作成することです。

代わりに、ファクトリパターン(パラメータに応じて異なるオブジェクトをインスタンス化して返す静的メソッド)を使用する必要があります。

public static Deck FromType(String type)
{
    if (type.Equals("Standard", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 60);
    }
    else if (type.Equals("Extended", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 60);
    }
    else if (type.Equals("Modern", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 60);
    }
    else if (type.Equals("Commander", StringComparison.OrdinalIgnoreCase)|| type.Equals("EDH", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 100);
    }

    throw new ArgumentOutOfRangeException("Bad type");
}

その後、キーワードtoTest = Deck.FromType(Console.ReadLine())を使用する代わりに、を使用して新しいデッキをインスタンス化できます。new

于 2012-09-28T03:07:58.460 に答える
2

オブジェクトを取得するのに最適な方法ではないと思います。次のようなことを試してみてください。

public class Deck
{
    public static Deck GetDeck(String type)
    {
        if (type.Equals("Standard", StringComparison.OrdinalIgnoreCase))
        {
            return new Deck(type, 60);
        }
        else if (type.Equals("Extended", StringComparison.OrdinalIgnoreCase))
        {
            return new Deck(type, 100);
        }
    }
}

そして、このようにデッキを取得するよりも:

Deck.GetDeck("foo");
于 2012-09-28T03:12:07.863 に答える
1

別のコンストラクターを呼び出すには、メソッドの内部ではなく、メソッドの本体の前に呼び出します。

public Deck(String type) : this(type,
  type.Equals("Standard", StringComparison.OrdinalIgnoreCase) ||
  type.Equals("Extended", StringComparison.OrdinalIgnoreCase) ||
  type.Equals("Modern", StringComparison.OrdinalIgnoreCase)
? 60 : 100)
{
  // nothing more here
}
于 2012-09-28T03:16:23.323 に答える