C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Index ALL OF A SUDDEN out of bounds of Array Posted by tvance929 on 30 Aug 2006 at 11:25 AM
Ok, its me again. I am trying to mold my brain into thinking in Codese and I thought I had a decent way of creating a Deck of Cards.

Everything was working and seem to be going fine when BLAM! - I get an out of bounds of array exception.

I have two arrays (One with the Suits of the cards and one with the Numbers or Ranks of the Cards) -- then I was iterating through them and creating a new object that added the two arrays together.

**BTW - I am SURE there are a million better ways to do what I am trying but I am trying to create this with what I have right now in my brain**

Anyway, I create the object then write it to the screen and begin the next iteration. It was filling up nicely -- THREE SUITS were completely filled up and the fourth one was filling up BUT when it gets to the 10th value of the Rank array I get an Exception - Index out of bounds of Array - error.

It doesnt make any sense to me because I have gone BEYOND the 10th value 3 times already in the other suits. --

--- CAN SOMEONE give me a clue why I am getting this? ---
============
here is the code.

namespace ConsoleApplication1
{
{
class card
{
public string suit = "Clubs";
public string rank = "One";
public string[] Suits = new string[] { "h", "d", "c", "s" };
public string[] Ranks = new string[] { "A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };



}
}

class Program
{


static void Main(string[] args)
{

card mycard = new card();

string card = mycard.rank + " of " + mycard.suit;
string[] cards = new string[52];


int s;
int r;
int i = 0;

for (s = 0; s < 5; s++)
{
for (r = 0; r < 14; r++)
{

cards[i] = mycard.Suits[s] + " of " + mycard.Ranks[r];
Console.WriteLine(cards[i]);

i++;
}
}







}
}
}

Report
Re: Index ALL OF A SUDDEN out of bounds of Array Posted by tvance929 on 30 Aug 2006 at 11:38 AM
OK!!! -- Moron here! -- I found the OBVIOUS ANSWER!

Just in case someone wants some fun and couldn't figure it out, I will put it down a little below so you can think about it.


















Here's a hint ... have you ever seen the 1 of Clubs? or 1 of anything?!?

I had the Ranks listing an "A" and a "1" as well -- thats the same card dorkwad!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.