Lewis Thomson
Lewis Thomson

Reputation: 5

How to set a required reasource location as a variable?

I'm currently making a game for an Advanced Higher computing course. After having my previous question answered perfectly and helping me a great bunch. I was wondering if you could share you wisdom upon me once again...

I've currently declared the variables, got the selection process narrowed down. All I'm sturggling to do is for it to recognise it's a variable and not the location itself.

UsedTile = My.Resources.Token(Turn)
CurrentTile = My.Resources.Colour(Turn)

I should also mention that UsedTile and CurrentTile are declared as Image so that it works with the rest of my progam.

Token is an array that swaps between 1 & 2 which is defined by Turn.

Sorry if i'm explaining this baddly, but basically I would want it to look something like

UsedTile = My.Resources.(Colour(Turn))

So that it is interchangable. All of the reasources are there, so for instance it would be a red tile it should show as

UsedTile = My.Resources.ColourRed.png

Thanks, I hope I've made it understandable. I'd happily upload more code if needed :)

-Lewis

Upvotes: 0

Views: 48

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

It's a little unclear what it is that you are asking, but here are two suggestions that may help you. First, you may want to consider reading the resources once and storing them in some other data structure that makes them more convenient to access when you need them later. For instance, if you created a class like this:

Public Class ColourResources
    Public Property TurnImages As Image(2)
End Class

Then you could create an array of them and fill them like this:

Dim colors(2) As ColourResources
colors(0).TurnImages(0) = My.Resources.ColourRed
colors(0).TurnImages(1) = My.Resources.ColourRedUsed
colors(1).TurnImages(0) = My.Resources.ColourBlack
colors(1).TurnImages(1) = My.Resources.ColourBlackUsed

Then when you need a particular image, you could just access in some way similar to this:

Dim tile As Image = colors(currentColor).TurnImages(currentTurn)

If your colors and turns are kept track of with some data type other than an Integer, you could use dictionaries instead of arrays.

My second suggestion is that it is possible to get the resource by string name rather than via resource-designer-auto-generated property. For instance, instead of this:

UsedTile = My.Resources.ColourRed

You could also access the same image like this:

UsedTile = DirectCast(My.Resources.ResourceManager.GetObject("ColourRed"), Image)

Depending on your needs, that may be useful to you.

Upvotes: 1

Related Questions