我写的CardExample一个类工程如下
Card.cs
System;
using System.Collections;
using System.Text;
namespace CardExample
{
public enum Suit
{
红心,
方块,
梅花,
黑桃
}
public enum Rank
{
A=1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
_10,
J,
Q,
K
}
public class Card
{
public readonly Suit suit;
public readonly Rank rank;
public override string ToString()
{
return ""+suit+rank;
}
private Card()
{
}
public Card(Suit newSuit, Rank newRank)
{
suit = newSuit;
rank = newRank;
}
}
}
Cards.cs
using System;
using System.Collections;
using System.Text;
namespace CardExample
{
public class Cards:CollectionBase
{
public void Add(Card newCard)
{
List.Add(newCard);
}
public void Remove(Card oldCard)
{
List.Remove(oldCard);
}
public Cards()
{
}
public Card this[int cardIndex]
{
set
{
return (Card)List[cardIndex];
}
get
{
List[cardIndex]= value;
}
}
public bool Contains(Card card)
{
return InnerList.Contains(card);
}
}
}
Deck.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace CardExample
{
public class Deck
{
private Cards cards = new Cards();
public Deck()
{
for(int suitVal=0;suitVal<4;suitVal++)
{
for (int rankVal = 1; rankVal < 14;rankVal++ )
{
cards.Add(new Card((Suit)suitVal, (Rank)rankVal));
}
}
}
public Card GetCard(int cardNum)
{
if (cardNum >= 0 && cardNum <= 51)
{
return cards[cardNum];
}
else
{
throw (new System.ArgumentOutOfRangeException());
}
}
public void Shuffle()
{
Cards newDeck = new Cards();
bool[] assigned= new bool[52];
for (int i = 0; i < 52; i++)
{
int soureeCard = 0;
bool foundCard = false;
Random newRandom = new Random();
while(foundCard==false )
{
soureeCard = newRandom.Next(52);
if (assigned[soureeCard] == false)
foundCard = true;
}
assigned[soureeCard] = true;
newDeck.Add(cards[soureeCard]);
}
cards = newDeck;
}
}
}
在编译成.dll文件时候出现两个错误:
错误 1 由于“CardExample.Cards.this[int].set”返回 void,返回关键字后面不得有对象表达式 E:\备份\作业练习\Visual Studio 2005\Projects\CardExample\CardExample\Cards.cs 25 17 CardExample
错误 2 当前上下文中不存在名称“value” E:\备份\作业练习\Visual Studio 2005\Projects\CardExample\CardExample\Cards.cs 29 34 CardExample
由于我是刚刚自学,还请老鸟指点!