vb.net - How to select an image from resources via a string? -
i'm coding pokedex type deal practice class. basically, have class titled "pokemon". 1 of properties of class "imgname" want use display image resources same name.
vb doesn't allow me call imgname string , use 'my.resources.imgname'
how can this, or alternative options it? want determined property in pokemon object, , don't want have hard code in if-elseif statement every single pokemon.
one way can have resource file added project. drop resource it. able address this:
my.resources.resource1.imgname
resource1
resource file name, , imgname
resource name here. need hard code every call. however, full intellisense support type checking.
if don't want hard code, here stripped down version of production code:
imports system.reflection imports system.xml.linq public class embeddedresourcemanager private class embeddedresourcemanagercore private shared _executingassembly assembly private shared _resourceprefix string shared sub new() _executingassembly = assembly.getexecutingassembly _resourceprefix = _executingassembly.getname.name & "." end sub public shared function getstream(resourcerelname string) io.stream return _executingassembly.getmanifestresourcestream(_resourceprefix & resourcerelname) end function end class public shared function getimage(byval resourcename string) bitmap return new bitmap(embeddedresourcemanagercore.getstream(resourcename)) end function end class
so whenever need, call embeddedresourcemanager.getimage
, pass resource name, appears in project (your image file needs attached project). need have build action
image in question set embedded resource
.
this piles resource executable, has both benefits , drawbacks, depending on situation. still, should work needs, since assuming number of different pokemons limited , not change throughout game (i.e. downloaded 3rd party server in real time etc.).
Comments
Post a Comment