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

99封情书 提交于 2019-12-25 05:28:09

问题


Ive always been able to just search for what I need on here, and I've usually found it fairly easily, but this seems to be an exception.

I'm writing a program in Visual Basic 2010 Express, it's a fairly simple text based adventure game.

I have a story, with multiple possible paths based on what button/option you choose. The text of each story path is saved in its own embedded resource .txt file. I could just write the contents of the text files straight into VB, and that would solve my problem, but that's not the way I want to do this, because that would end up looking really messy.

My problem is that I need to use variable names within my story, here's an example of the contents of one of the embedded text files,

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

I have used the following code to read the file into my text box

Private Sub frmAdventure_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim thestorytext As String
    Dim imageStream As Stream
    Dim textStreamReader As StreamReader
    Dim assembly As [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 to an extent, but displays it exactly as it is in the text file, keeps the quotes and the +s and the variable names instead of removing the quotes and the +s and replacing the variable names with what's stored within the variables.

Can anyone tell me what I need to change or add to make this work?

Thanks, and apologies if this has been answered somewhere and I just didn't recognise it as the solution or didn't know what to search to find it or something.


回答1:


Since your application is compiled, you cannot just put some of your VB code in the text file and have it executed when it is read.

What you can do, and what is usually done, is that you leave certain tags inside your text file, then locate them and replace them with the actual values.

For example:

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

Then in your code, you would find all the tags:

Dim matches = Regex.Matches(thestorytext, "%(\w+?)%")
For Each match in matches
    ' the tag name is now in: match.Groups(1).Value
    ' replace the tag with the value and replace it back into the original string
Next

Of course the big problem still remains - which is how to fill in the actual values. Unfortunately, there is no clean way to do this, especially using any local variables.

You can either manually maintain a Dictionary of tag names and their values, or use Reflection to get the values directly at the runtime. While it should be used carefully (speed, security, ...), it will work just fine for your case.

Assuming you have all your variables defined as properties in the same class (Me) as the code that reads and processes this text, the code will look like this:

Dim matches = Regex.Matches(thestorytext, "%(\w+?)%")
For 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 you don't use properties, but only fields, change the line to this:

Dim value = Me.GetType().GetField(tag).GetValue(Me)

Note that this example is rough and the code will happily crash if the tags are misspelled or not existing (you should do some error checking), but it should get you started.



来源:https://stackoverflow.com/questions/16336897/visual-basic-2010-using-variables-in-embedded-text-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!