-
Jan 20th, 2025, 07:48 AM
#1
Thread Starter
Fanatic Member
[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
-
Jan 20th, 2025, 08:16 AM
#2
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 20th, 2025, 08:28 AM
#3
Thread Starter
Fanatic Member
-
Jan 20th, 2025, 08:35 AM
#4
Re: condense code
 Originally Posted by .paul.
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.
-
Jan 20th, 2025, 11:38 AM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|