ここに学生がいます。リスト< >辞書の中に< K,List< >>(C#)
現在のC#プロジェクトで作業しています。私はトランプカードのリスト<カードを作成し、辞書<ストリング、リスト<カード>を作成しなければなりません。
リストは個別に作成され、辞書はカードのコレクションを保持します。コレクションからカードを追加または削除することができます。
コレクションにカードを追加するのに問題があります。ユーザー入力からリストのインデックス番号を取得しましたが、インデックスに基づいて辞書の値を単純に更新しようとしています。
エラーを投げている行はここにあります。エラーは私の人生のために私は何をその手段と方法を見つけ出すことはできません が暗黙のうちに System.Collections.Generic.List < filename.CardClass>」
に型「< filename.CardClassを>」に変換できませんでそれを解決するために...ここで
_collections[input] = _cards[cardSelectionIndex];
は私の現在のコードブロックです:
public void AddToCollection()
{
PrintValuesCollections<string, List<Card>>(_collections);
Console.WriteLine("Type in the name of the collection to add a card to:");
string input = Console.ReadLine();
bool found = _collections.ContainsKey(input);
if (found)
{
Console.WriteLine("List of current cards to choose from:");
for (int i = 0; i < _cards.Count; i++)
{
Console.WriteLine("Index number:\r\n"+i+"Card Info:\r\n"+_cards[i]+"\n");
}
Console.WriteLine("Type in the index of the card from the list to add to the collection:");
string cardSelection = Console.ReadLine();
int cardSelectionIndex = 0;
while (!int.TryParse(cardSelection, out cardSelectionIndex))
{
Console.Write("Please enter a number from the list above: ");
input = Console.ReadLine();
}
_collections[input] = _cards[cardSelectionIndex];
_cards.RemoveAt(cardSelectionIndex);
}
else
Console.WriteLine("Collection name not found.");
}
'_collections [入力] .Add(_cards [cardSelectionIndex]);'? – flakes
わかりませんが、これで新しいユニークキーが追加されませんか?私は以前に言及したはずですが、私は既に作成されたキーを持っていて、ユーザーからのキー入力に基づいて値を更新したいだけです。 –