Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Visual Basic(2010)-在嵌入式文本文件中使用变量?_Vb.net_Variables_Embedded Resource - Fatal编程技术网

Vb.net Visual Basic(2010)-在嵌入式文本文件中使用变量?

Vb.net Visual Basic(2010)-在嵌入式文本文件中使用变量?,vb.net,variables,embedded-resource,Vb.net,Variables,Embedded Resource,我总是能够在这里搜索我需要的东西,我通常很容易找到,但这似乎是个例外 我正在用VisualBasic2010Express编写一个程序,这是一个相当简单的基于文本的冒险游戏 我有一个故事,根据您选择的按钮/选项,有多个可能的路径。 每个故事路径的文本保存在其自己的嵌入式resource.txt文件中。我可以直接把文本文件的内容写进VB,这样就解决了我的问题,但我不想这样做,因为那样会看起来很混乱 我的问题是我需要在我的故事中使用变量名,下面是一个嵌入文本文件内容的示例 "When "+playe

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

我正在用VisualBasic2010Express编写一个程序,这是一个相当简单的基于文本的冒险游戏

我有一个故事,根据您选择的按钮/选项,有多个可能的路径。 每个故事路径的文本保存在其自己的嵌入式resource.txt文件中。我可以直接把文本文件的内容写进VB,这样就解决了我的问题,但我不想这样做,因为那样会看起来很混乱

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

"When "+playername+" woke up, "+genderheshe+" didn't recognise "+genderhisher+" surroundings."
我使用以下代码将文件读入文本框

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
它在某种程度上起作用,但显示方式与文本文件中的显示方式完全相同,保留引号和+s以及变量名,而不是删除引号和+s,并用存储在变量中的内容替换变量名

有人能告诉我需要做哪些更改或添加才能使这项工作正常进行吗


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

由于您的应用程序是编译的,您不能将一些VB代码放在文本文件中,并在读取时执行它

您可以做的,通常也可以做的,就是在文本文件中保留某些标记,然后找到它们并用实际值替换它们

例如:

When %playername% woke up, %genderheshe% didn`t recognise %genderhisher% surroundings.
然后在代码中,您将找到所有标记:

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
当然,最大的问题仍然存在——那就是如何填写实际值。不幸的是,没有干净的方法来做到这一点,特别是使用任何局部变量

您可以手动维护标记名及其值的
字典,也可以在运行时使用反射直接获取值。虽然它应该小心使用(速度、安全性等),但对于您的情况来说,它会很好地工作

假设将所有变量定义为与读取和处理此文本的代码相同的类(
Me
)中的属性,则代码如下所示:

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
如果不使用属性,而仅使用字段,请将行更改为:

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

请注意,这个示例很粗糙,如果标记拼写错误或不存在,代码将很高兴崩溃(您应该做一些错误检查),但它应该可以帮助您开始。

谢谢!我没有想过使用正则表达式,但我想这正是它创建的目的。一个关于正则表达式构造的简短教程之后,我就走了!多莫·阿里加托。对于那些感兴趣的人,以下是我与此结合使用的资源,以更好地了解正在发生的事情: