Lord Lord - 7 months ago 17
Vb.net Question

Need help shortening repetitive vb code

I'm pretty new to programming, so this may be a pretty basic question. But, I need help to shorten this really repetitive code.
I'm working on a card game that has about 2 hundred different creatures, two decks (your and the enemy's), and 10 slots in each deck (meaning 10 creatures per deck).

In the deck builder part of my UI, when you actually add a creature to your deck, it does this:

If CritName = "Monarch" Then
YourCreature1PictureDB.Image = DHBattleSim.My.Resources.Monarch_Icon
YourCreature1Group.BackColor = Color.Transparent
End If


If the creature is "Ariel", then it'd be

If CritName = "Ariel" Then
YourCreature1PictureDB.Image = DHBattleSim.My.Resources.Ariel_Icon
YourCreature1Group.BackColor = Color.Transparent
End If


etc. Now, imagine there being two hundred of those statements, each for a different creature. THEN I need to copy that huge chunk of code, and change all of the 1's to 2's, and again, changing the 2's to 3's, etc. After I finish the 10's, I'd have to copy ALL OF THAT and change all of the "YourCreature" phrases to "EnemyCreature". So obviously this is extremely repetitive and tedious.
I know about the Find and Replace feature, but I'd rather shorten the code itself so that I don't have to resort to using that.

Answer

You can use code like this

Dim resources As Object = DHBattleSim.My.Resources.ResourceManager
YourCreature1PictureDB.Image = resources.GetObject(yourVariable & "_Icon")

This,

YourCreature1Group.BackColor = Color.Transparent

can follow; in your original code it doesn't need to appear in every if, if it is always set to Transparent.