Solved

subtracting string varaibles from an array.

Posted on 2010-11-08
10
260 Views
Last Modified: 2013-11-26
Hi Everyone,

I have an array:

Dim cardDeck() As String = {"h2", "d2", "c2", "s2", "h3", "d3", "c3", "s3", "h4", "d4", "c4", "s4", "h5", "c5", "d5", "s5", "h6", "d6", "c6", "s6", "h7", "d7", "c7", "s7", "h8", "c8", "d8", "s8", "h9", "c9", "d9", "s9", "ht", "ct", "dt", "st", "hj", "cj", "dj", "sj", "hq", "cq", "dq", "sq", "hk", "ck", "dk", "sk", "ha", "ca", "da", "sa"}

and two string variables temp1 and temp2 with values such as d4 and s3

Now i want to delete these two variables from the array cardDeck.

Please help deleting these two variables and storing the remaining array in a new array.

Thanks
0
Comment
Question by:justind39
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34089903
Instead of an Array, use List(Of String).  This will allow you to remove the items without having to declare another array and copy everything over...
0
 
LVL 1

Author Comment

by:justind39
ID: 34090003
Hi,

Thank you for replying. I read the same thing few minutes ago that arrayLists are more flexible but I am worried that it will affect logic of the rest of my program.

My current code is attached. I have already declared temp and temp2.
Could you help me modify it using list (of String)?

I appreciate your help.
Public Sub Deal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button39.Click
        My.Computer.Audio.Play(Application.StartupPath & "\deal.wav")
        Dim cardDeck() As String = {"h2", "d2", "c2", "s2", "h3", "d3", "c3", "s3", "h4", "d4", "c4", "s4", "h5", "c5", "d5", "s5", "h6", "d6", "c6", "s6", "h7", "d7", "c7", "s7", "h8", "c8", "d8", "s8", "h9", "c9", "d9", "s9", "ht", "ct", "dt", "st", "hj", "cj", "dj", "sj", "hq", "cq", "dq", "sq", "hk", "ck", "dk", "sk", "ha", "ca", "da", "sa"}
        Dim ListOfCards As List(Of String) = cardDeck.ToList
        Dim Count As Integer = ListOfCards.Count
        Dim MyBoxes(4) As PictureBox
        Dim xLoc As Integer = 393
        Dim yLoc As Integer = 499
        Dim Selected As String
        Dim PlayerCards As String = "Player Cards" & vbCrLf
        Dim AllCards As String = ""

        For i As Integer = 0 To 4
            Count = ListOfCards.Count
            Selected = Rndm.Next(Count)
            PlayerCards = PlayerCards & ListOfCards(Selected).ToString & vbCrLf
            MyBoxes(i) = New PictureBox
            MyBoxes(i).Width = 187
            MyBoxes(i).Height = 261
            MyBoxes(i).Image = Image.FromFile("cards_bmp/" + ListOfCards(Selected) + ".png")
            MyBoxes(i).Location = New System.Drawing.Point(xLoc, yLoc)
            xLoc += 210

            Me.Controls.Add(MyBoxes(i))
            ListOfCards.RemoveAt(Selected)


        Next
        yLoc = yLoc + 120
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:justind39
ID: 34090016
Or do you think is there any other way in the ListOfCards, that I can remove the cards with string value temp and temp2 to show up?
0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 36

Expert Comment

by:Miguel Oz
ID: 34090044
Replace line 4:
Dim ListOfCards As List(Of String) = cardDeck.ToList

with:
Dim ListOfCards As List(Of String) = New List(Of String)(cardDeck)

you can setup the list with an array, then use the index to access the values.
Note replace ListOfCards(Selected).ToString with ListOfCards(Selected) no need for conversion because it is already a string
0
 
LVL 3

Accepted Solution

by:
paul_p_vargas earned 500 total points
ID: 34090062

            Dim cardDeck() As String = {"h2", "d2", "c2", "s2", "h3", "d3", "c3", "s3", "h4", "d4", "c4", "s4", "h5", "c5", "d5", "s5", "h6", "d6", "c6", "s6", "h7", "d7", "c7", "s7", "h8", "c8", "d8", "s8", "h9", "c9", "d9", "s9", "ht", "ct", "dt", "st", "hj", "cj", "dj", "sj", "hq", "cq", "dq", "sq", "hk", "ck", "dk", "sk", "ha", "ca", "da", "sa"}
            Dim temp1 As String
            temp1 = "d4"
            Dim temp2 As String
            temp2 = "s3"

            Dim cardList As New Generic.List(Of String)

'convert Array to List
            cardList.AddRange(cardDeck)  

'remove the 2 items
            cardList.Remove(temp1)
            cardList.Remove(temp2)
0
 
LVL 1

Author Comment

by:justind39
ID: 34090094
@paul_p_vargas - I tried ur solution but it gives me this error:

Warning      1      Variable 'cardList' is used before it has been assigned a value. A null reference exception could result at runtime.      C:\Users\ankit\Desktop\cevpoker\cevpoker\Form1.vb      94      9

on this line :

cardList.AddRange(cardDeck)
0
 
LVL 3

Expert Comment

by:paul_p_vargas
ID: 34090100
have you tried copying and pasting the whole code? i tried it and it works on my pc.
make sure there is teh "New" keyword in declaring your variable.
0
 
LVL 1

Author Comment

by:justind39
ID: 34090115
Awesome! cool thanks alot. It works..
0
 
LVL 1

Author Closing Comment

by:justind39
ID: 34090121
Awesome work!
0
 
LVL 9

Expert Comment

by:shadow77
ID: 34090183
If you want to do this with an array, this will work:

    Private Sub Test09()
        Dim cardDeck() As String = {"h2", "d2", "c2", "s2", "h3", "d3", "c3", "s3", "h4", "d4", "c4", "s4", "h5", "c5", "d5", "s5", "h6", "d6", "c6", "s6", "h7", "d7", "c7", "s7", "h8", "c8", "d8", "s8", "h9", "c9", "d9", "s9", "ht", "ct", "dt", "st", "hj", "cj", "dj", "sj", "hq", "cq", "dq", "sq", "hk", "ck", "dk", "sk", "ha", "ca", "da", "sa"}
        RemoveCard(cardDeck, "d4")
        RemoveCard(cardDeck, "s3")
    End Sub

    Private Sub RemoveCard(ByRef cardDeck() As String, ByVal card As String)
        Dim n As Integer = cardDeck.Length
        Dim k As Integer = Array.IndexOf(cardDeck, card)
        If k >= 0 Then
            Array.Copy(cardDeck, k + 1, cardDeck, k, n - k - 1)
            ReDim Preserve cardDeck(n - 2)      ' subtract 1 to reduce array size; subtract another 1 because indexes start at 0
        End If
    End Sub

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question