Hi, beginner in vb here, I'm trying to write a game of blackjack in vb.net, and I'm currently trying to set up a subroutine that will check to see if a card matches another card already dealt, and if so deal a different card (to avoid dealing the same card twice, as with a real deck). Here's the relevant code to this part:

Dim notDealt As Boolean
Dim cardDealt(9) As Short
Dim rand_deck1 As New Random
Dim card_check As Short
Dim deck1(51) As Integer



Public Sub btn_hit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_hit.Click

        'hit_count is a variable that tracks how many hits the player has elected to take
        'this segment deals hit cards to the player and checks which card it is dealing
        If hit_count = 0 Then
            notDealt = False
            Do Until notDealt = True
                cardDealt(2) = rand_deck1.Next(52)
                card_checker(2, 2)
            Loop
End Sub

Public Sub card_checker(ByVal card_compared As Integer, ByVal card_checked As Integer)
        Dim a_total As Short = 0
        Do Until card_compared < 0
            card_compared = card_compared - 1
            If cardDealt(card_checked) = cardDealt(card_compared) Then
                a_total = a_total + 1
            Else
                a_total = a_total
            End If
        Loop
        If a_total = 0 Then
            notDealt = True
        Else
            notDealt = False
        End If
    End Sub

However, upon clicking the hit button, I get this error


An unhandled exception of type 'System.IndexOutOfRangeException' occurred in blackjack.exe

Additional information: Index was outside the bounds of the array.

With this line highlighted:

If cardDealt(card_checked) = cardDealt(card_compared) Then

Does anybody know how I would fix this? I haven't been able to figure out the problem on my own. I'm using visual studio 2008, if that's necessary info.

Thanks to anyone that can help.

Recommended Answers

All 4 Replies

Try this:

Do Until card_compared = 0

That's fixed it, thank you very much.

hi, i am new to asp.net. I have developed a map guide. when i login in the page it shows me a Index was outside the bounds of the array.
I dont know how to fix it. can anyone help me.
here is the code.

 protected void Page_Load(object sender, EventArgs e)
        {
            string user_logon = Request.ServerVariables["LOGON_USER"];
            char[] separator = new char[] { '\\' };
            string[] keywords_split = user_logon.Split(separator);
            Re_Usable rs = new Re_Usable();
            DataSet user_ds = rs.getUserFullInfo(keywords_split[1].Trim());
            if (user_ds.Tables["User"].Rows.Count <= 0)
            {
                errorLabel.Text = "Authentication did not succeed. Check user name." + user_logon;
                return;
            }
            else
            {
                UserDetail ud = new UserDetail();
                foreach (DataRow dr in user_ds.Tables["User"].Rows)
                {
                    ud.UserName = dr["username"].ToString();
                    ud.AdminStatus = dr["admin"].ToString();
                    ud.AccessStatus = dr["status"].ToString();
                    ud.FullName = dr["name"].ToString();
                }

                Hashtable cl;
                if (user_ds.Tables["CategoryList"].Rows.Count > 0)
                {
                    cl = new Hashtable(user_ds.Tables["CategoryList"].Rows.Count);
                    foreach (DataRow dr2 in user_ds.Tables["CategoryList"].Rows)
                    {
                        cl.Add(dr2["CATEGORYID"].ToString(), "checked");
                    }
                    ud.CategoryList = cl;
                }

                if (!ud.AccessStatus.ToString().Equals("A"))
                {
                    errorLabel.Text = "Authentication did not succeed. Check user name." + user_logon;
                    return;
                }
                else
                {
                    Session["userdetail"] = new UserDetail();
                    Session["userdetail"] = ud;
                    Session.Timeout = 60;
                    Session["username_info"] = ud.UserName;

                    switch (ud.AdminStatus.ToString())
                    {
                        case "Y":
                            Response.Redirect(HttpRuntime.AppDomainAppVirtualPath + "/Admin/Map/map_tool.aspx");
                            return;

                        default:
                            Response.Redirect(HttpRuntime.AppDomainAppVirtualPath + "/Users/Map/map_tool.aspx");
                            return;
                    }
                }
            }
        }

the error is showed in

Line 25:             DataSet user_ds = rs.getUserFullInfo(keywords_split[1].Trim());

please help me. Thank you.

Hi, WEhen I click on Login link in asp.net it shows me "index was outside the bounds of array".

Can any one help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.