Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Read Commands From A File

Hello, I Need to Know How To Read VB 2008 Express Commands From A File (.txt), Run it, And Return It To A Variable
Diagram
vbreader.jpg
I Tried Searching On The Internet But There Was Nothing.
Please Help!
 
There's a few ways to do that, some are easier than others.

First off, are you asking if you can arbitrarily execute VB code from a text file?

Visual Basic files require more structure than just 'Dim x as String', so if you wanted to use Visual Basic's compiler to do the work for you, you'd have to do some extra work, encase the code in a class and then a subroutine of that class.

If you want more control you could go the interpreter route, which is harder. Either find or create a lexer (text analysis) and parser (grammar analysis) for the language you want to use, then run through the parse graph one or more times to verify the semantics (in other words, what it means). Then use the parse graph (or whatever you turned it into with one of the other semantic passes) and interpret its code by evaluating the meaning of the individual symbols, such as what methods/assignments are mentioned in the file.
 
I haven't made anything in VB.net for a while, so maybe I make some mistakes, but based on what I have read around internet this may work:

Code:
Imports System.IO

 

Public Function ReadFileAndEval(FullPath As String) As Object

  Dim error As String

  Dim text As String

  Dim evalText As New ScriptControl

  Dim Result As Object

  Dim objReader As StreamReader

 

  Try

 

    objReader = New StreamReader(FullPath)

    text = objReader.ReadToEnd()

    objReader.Close()

 

    Result = evalText.Eval(text)

 

  Catch Ex As Exception

    'Error Here

  End Try

 

  Return Result

End Function

You read the contents of a file with:
Code:
    objReader = New StreamReader(FullPath)

    text = objReader.ReadToEnd()

    objReader.Close()

And, you eval a text with:
Code:
Result = evalText.Eval(text)
Where evalText is a ScriptControl object.
 
It Says Script Control Is Not Defined! Where Is It?
EDIT:Here Is the Code(It's In A Class Library Named IOSVB)
Code:
<div class="vb" id="{CB}" style="font-family: monospace;"><ol>Imports System.IO

<span style="color: #b1b100;">Public Class RoniaIO

    <span style="color: #b1b100;">Public <span style="color: #b1b100;">Function ReadFileAndEval(ByVal FullPath <span style="color: #b1b100;">As <span style="color: #b1b100;">String) <span style="color: #b1b100;">As Object

        <span style="color: #b1b100;">Dim <span style="color: #b1b100;">err <span style="color: #b1b100;">As <span style="color: #b1b100;">String

        <span style="color: #b1b100;">Dim text <span style="color: #b1b100;">As <span style="color: #b1b100;">String

        <span style="color: #b1b100;">Dim evalText <span style="color: #b1b100;">As <span style="color: #b1b100;">New [color=#FF0000]ScriptControl[/color]<span style="color: #808080;">'Where I Get The Error

        <span style="color: #b1b100;">Dim Result <span style="color: #b1b100;">As Object

        <span style="color: #b1b100;">Dim objReader <span style="color: #b1b100;">As StreamReader

 

        Try

 

            objReader = <span style="color: #b1b100;">New StreamReader(FullPath)

            text = objReader.ReadToEnd()

            objReader.<span style="color: #b1b100;">Close()

 

            Result = evalText.Eval(text)

 

        Catch Ex <span style="color: #b1b100;">As Exception

            <span style="color: #808080;">'Error Here

        <span style="color: #b1b100;">End Try

 

        <span style="color: #b1b100;">Return Result

    <span style="color: #b1b100;">End <span style="color: #b1b100;">Function

<span style="color: #b1b100;">End Class

 
Off Topic:
The Hi-lighting Was Done With
Code:
[code=vb]

[/code] for People Who Don't Know
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top