Visual Basic(2010)-在嵌入式文本文件中使用变量? [英] Visual Basic (2010) - Using variables in embedded text files?

查看:89
本文介绍了Visual Basic(2010)-在嵌入式文本文件中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直能够在这里搜索我需要的东西,而且我通常很容易找到它,但这似乎是个例外。



我正在Visual Basic 2010 Express中编写一个程序,它是一个非常简单的基于文本的冒险游戏。



我有一个故事,根据哪个按钮/您选择的选项。
每个故事路径的文本都保存在其自己的嵌入式资源.txt文件中。我可以直接将文本文件的内容写到VB中,这可以解决我的问题,但是那不是我想要的方式,因为这最终看起来会很混乱。 p>

我的问题是我需要在故事中使用变量名,这是一个嵌入式文本文件内容的示例,

 当 +玩家名+醒来时, + genderheshe +无法识别 + genderhisher +环境。 

我已使用以下代码将文件读取到我的文本框中

  Private Sub frmAdventure_Load(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load 
昏暗thestorytext作为字符串
Dim imageStream作为流
Dim textStreamReader作为StreamReader
Dim程序集作为[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

在某种程度上可以正常工作,但是与文本文件中的内容完全一样用引号和+ s和变量名代替删除引号和+ s并替换变量用存储在变量中的名字来命名。



有人可以告诉我我需要更改或添加什么才能使它起作用吗?



谢谢,如果在某个地方已经回答了这个问题,我深表歉意,而我只是不认为它是解决方案,或者不知道要搜索什么来找到它或其他东西。

解决方案

由于已经编译了应用程序,因此您不能仅将一些VB代码放入文本文件中并在读取时执行它。



可以可以做什么,通常要做的是,在文本文件中保留某些标签,然后找到并替换它们



例如:

 当%playername %醒来了,%genderheshe%无法识别%genderhisher%的环境。 

然后在代码中,您会找到所有标签:

 暗匹配= Regex.Matches(thestorytext,%(\w +?)%)
对于匹配中的每个匹配
'标记名称现在位于:match.Groups(1).Value
'将标记替换为值,然后将其替换回原始字符串
下一个

当然,最大的问题仍然存在-如何填写实际值。不幸的是,没有干净的方法可以做到这一点,尤其是使用任何局部变量。



您可以手动维护 Dictionary 标签名称及其值,或使用 Reflection 直接在运行时获取值。



假设您已将所有变量定义为属性,则应谨慎使用它(速度,安全性,...),但对您的情况来说将是正确的。与读取和处理此文本的代码相同的类( Me ),代码将如下所示:

 暗匹配= Regex.Matches(thestorytext,%(\w +?)%)
对于匹配中的每个匹配
Dim标签= match.Groups( 1).Value
Dim值= Me.GetType()。GetField(tag).GetValue(Me)
thestorytext = thestorytext.Replace(match.Value,value)'惰性代码
接下来

txtAdventure.Text = thestorytext

如果您不使用属性,但仅限字段,将行更改为此:

  Dim值= Me.GetType()。GetField(tag).GetValue(我)

请注意,此示例很粗糙,无论标签是否拼写错误,代码都会很容易崩溃现有(您应该进行一些错误检查),但是我t应该可以帮助您入门。


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.

解决方案

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.

这篇关于Visual Basic(2010)-在嵌入式文本文件中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆