Results 1 to 5 of 5

Thread: [RESOLVED] condense code

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    Resolved [RESOLVED] condense code

    Is it possible to rewrite this code in fewer lines?
    Code:
            PictureBox1.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(0))), Image)
            PictureBox2.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(1))), Image)
            PictureBox3.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(2))), Image)
            PictureBox4.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(3))), Image)
            PictureBox5.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(4))), Image)
    
            PictureBox6.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(5))), Image)
            PictureBox7.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(6))), Image)
            PictureBox8.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(7))), Image)
            PictureBox9.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(8))), Image)
            PictureBox10.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(9))), Image)
    
            PictureBox11.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(10))), Image)
            PictureBox12.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(11))), Image)
            PictureBox13.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(12))), Image)
            PictureBox14.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(13))), Image)
            PictureBox15.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(14))), Image)
    
            PictureBox16.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(15))), Image)
            PictureBox17.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(16))), Image)
            PictureBox18.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(17))), Image)
            PictureBox19.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(18))), Image)
            PictureBox20.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(19))), Image)
    
            PictureBox21.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(20))), Image)
            PictureBox22.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(21))), Image)
            PictureBox23.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(22))), Image)
            PictureBox24.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(23))), Image)
            PictureBox25.Image = CType(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(24))), Image)
    Thank you

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,942

    Re: condense code

    Code:
    For x As Integer =0 to 24
        Dim pb As PictureBox= DirectCast(Me.Controls(“PictureBox” & (x+1).ToString), PictureBox)
        Dim img As Image = DirectCast(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(x))), Image)
        pb.Image =img
    Next
    Last edited by .paul.; Jan 21st, 2025 at 10:23 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    534

    Re: condense code

    Thank you so much!!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,981

    Re: condense code

    Quote Originally Posted by .paul. View Post
    Code:
    For x As Integer =0 to 24
        Dim pb As PictureBox= DirectCast(Me.Controls(“PictureBox” & (x+1).ToString, PictureBox)
        Dim img As Image = DirectCast(My.Resources.ResourceManager.GetObject(deckofcards(shuffledcards(x))), Image)
        pb.Image =img
    Next
    For the record, there's no need to call ToString there. That's why it's important to use & rather than + for concatenation in VB. + is the addition operator and will try to convert a String operand to a number in some cases but & will always convert supported operands to a String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,061

    Re: [RESOLVED] condense code

    A little more code cleanup. I wouldn't put so much chaining in a single line because its makes debugging more difficult in my opinion. I would also add some null checks and use String interpolation as well.
    Code:
    For x As Integer = 0 to 24
        Dim matchedControl As Control = Me.Controls($"PictureBox{x + 1}") ' if invalid syntax, use "PictureBox" & (x + 1)
        If (matchedControl IsNot Nothing) Then
            Dim matchedPictureBox As PictureBox = DirectCast(matchedControl, PictureBox)
            If (matchedPictureBox IsNot Nothing AndAlso shuffledcards.Contains(x)) Then
                Dim shuffledCard As Integer = shuffledcards(x)
                Dim cardLocation As String = deckofcards(shuffledCard)
                Dim matchedResource As Object = My.Resources.ResourceManager.GetObject(cardLocation)
                If (matchedResource IsNot Nothing) Then
                    Dim img As Image = DirectCast(matchedResource, Image)
                    matchedPictureBox.Image = img
                End If
            End If
        End If
    Next
    This is completely opinionated of course because it achieves basically the same thing that .paul.'s code does. The difference is that there are stopgaps in my code to prevent an unhandled exception from being thrown and if you need to step through you code, I think that my code is much easier to debug to see at what point the code fails.

    Edit - Come to think of it, I don't think String interpolation was available in 2010. If not, you should use the ampersand character like JMcIlhinney suggested.
    Last edited by dday9; Jan 20th, 2025 at 11:50 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width