vb.net - Visual Basic (2010) - Using variables in embedded text files? -


ive been able search need on here, , i've found easily, seems exception.

i'm writing program in visual basic 2010 express, it's simple text based adventure game.

i have story, multiple possible paths based on button/option choose. text of each story path saved in own embedded resource .txt file. could write contents of text files straight vb, , solve problem, that's not way want this, because end looking messy.

my problem need use variable names within story, here's example of contents of 1 of embedded text files,

"when "+playername+" woke up, "+genderheshe+" didn't recognise "+genderhisher+" surroundings." 

i have used following code read file text box

private sub frmadventure_load(byval sender system.object, byval e system.eventargs) handles mybase.load     dim thestorytext string     dim imagestream stream     dim textstreamreader streamreader     dim assembly [assembly]     assembly = [assembly].getexecutingassembly()     imagestream = assembly.getmanifestresourcestream("catastrophe.catastrophestorystart.png")     textstreamreader = new streamreader(assembly.getmanifestresourcestream("catastrophe.catastrophestorystart.txt"))     thestorytext = textstreamreader.readline()     txtadventure.text = thestorytext end sub 

which works extent, displays in text file, keeps quotes , +s , variable names instead of removing quotes , +s , replacing variable names what's stored within variables.

can tell me need change or add make work?

thanks, , apologies if has been answered somewhere , didn't recognise solution or didn't know search find or something.

since application compiled, cannot put of vb code in text file , have executed when read.

what can do, , done, leave tags inside text file, locate them , replace them actual values.

for example:

when %playername% woke up, %genderheshe% didn`t recognise %genderhisher% surroundings. 

then in code, find tags:

dim matches = regex.matches(thestorytext, "%(\w+?)%") each match in matches     ' tag name in: match.groups(1).value     ' replace tag value , replace original string next 

of course big problem still remains - how fill in actual values. unfortunately, there no clean way this, using local variables.

you can either manually maintain dictionary of tag names , values, or use reflection values directly @ runtime. while should used (speed, security, ...), work fine case.

assuming have variables defined properties in same class (me) code reads , processes text, code this:

dim matches = regex.matches(thestorytext, "%(\w+?)%") each match in matches     dim tag = match.groups(1).value     dim value = me.gettype().getfield(tag).getvalue(me)     thestorytext = thestorytext.replace(match.value, value) ' lazy code next  txtadventure.text = thestorytext 

if don't use properties, fields, change line this:

dim value = me.gettype().getfield(tag).getvalue(me) 

note example rough , code happily crash if tags misspelled or not existing (you should error checking), should started.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -