vb.net - Calling a variable from the data retrieved in a textfile -


i want reference values of variables have declared in code inside text file. want variables referenced in text file replaced appropriate values, depending on variables in code equal, having problems it.

the contents of text file, post.txt is:

i gonna display" & first & "and" & second & "text

here code:

dim test string dim first string = "first word" dim second string = "second word"  dim pstream new system.io.filestream("post.txt", io.filemode.open) dim preader new system.io.streamreader(pstream)  test = preader.readline()  pstream.close() preader.close()  sampletextbox.text = test 

i want sampletextbox display following:

i gonna display first word , second word text

but, instead, still displays being retrieved text file, is:

i gonna display" & first & "and" & second & "text

i have never tried calling variables text file, there way this?

the simple answer "no". variables not work that. variables way in code conveniently refer memory locations data stored. when source code built assembly, original source code no longer exists, part of resulting assembly.

you should not think of text files being extension of source code. text files way store raw data disk. data doesn't "run". data doesn't anything, in , of itself. data has no "access" source code. program must work if want or data.

so, if want that, you'll need parse , manipulate data via code write. how easy or difficult perform task depends entirely on how difficult file format parse , work with. format have chosen rather difficult, why else suggested regex. regex gives lot of power parse complicated data. however, if have control on format of data, far better approach change file format easier work with. use standard format xml. xml, choose format data in number of different ways, depending on needs. instance, format this:

<?xml version="1.0" encoding="utf-8"?> <document> gonna display <first/> , <second/> text. </document> 

or this:

<?xml version="1.0" encoding="utf-8"?> <document> gonna display <var name="first"/> , <var name="second"/> text. </document> 

or this:

<?xml version="1.0" encoding="utf-8"?> <document> gonna display <var>first</var> , <var>second</var> text. </document> 

then, use of xml tools in .net framework load xml document , replace variable elements proper values. in fact, use xslt, cool, digress.

i suspect, however, needs, changing format use simple placeholders, following, make more sense:

i gonna display %first% , %second% text.

then, replace placeholders in string variable values, this:

dim first string = "first word" dim second string = "second word" dim data string = system.io.file.readalltext("post.txt") data = data.replace("%first%", first) data = data.replace("%second%", second) sampletextbox.text = data 

Comments