Out of range when creating a list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Out of range when creating a list



No matter what I do, VisualStudio always highlights when I create shuffledDeck, indicating an OutOfRangeException. To my understanding this usually happens when you try referencing an element that does not exist. But i'm not referencing any element. Here is the error log:



System.ArgumentOutOfRangeException was unhandled
HResult=-2146233086
Message=Index was out of range. Must be non-negative and less than the size
of the collection.
Parameter name: index
ParamName=index
Source=mscorlib
StackTrace:
at
System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.Collections.Generic.List`1.get_Item(Int32 index)
at BlackJack.Program.shuffleDeck(Deck deck) in c:usersadministratordocumentsvisual studio 2015ProjectsBlackJackProgram.cs:line 42
at BlackJack.Program.Main(String args) in c:usersadministratordocumentsvisual studio 2015ProjectsBlackJackProgram.cs:line 16


class Program

static void Main(string args)

Deck deck = new Deck();
List<Card> shuffledDeck = new List<Card>();



class Card

public int Value

get; private set;

public enum suitType

clubs, spades, hearts, diamonds

public suitType Suit

get; private set;

public Card(int value, suitType suit)

Value = value;
Suit = suit;



class Deck

public static List<Card> unshuffledDeck get; set;

public Deck()

for (int j = 1; j < 14; j++)

unshuffledDeck.Add(new Card(j, Card.suitType.spades));
unshuffledDeck.Add(new Card(j, Card.suitType.diamonds));
unshuffledDeck.Add(new Card(j, Card.suitType.clubs));
unshuffledDeck.Add(new Card(j, Card.suitType.hearts));







Please edit your question to include the full error message including the stack trace.
– Progman
7 mins ago





You never initialize the unshuffledDeck property.
– Dai
4 mins ago


unshuffledDeck





Your code does not match the stack-trace. The stack-trace says there's a method Program.shuffledeck which does not exist in the code you posted.
– Dai
3 mins ago


Program.shuffledeck





even when I initialize, it gives me the same error
– SouR Graphics
3 mins ago





@SouRGraphics probably because when you (re)build your solution it fails, so it's executing the previous version.
– Erik Philips
59 secs ago










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard