Results 1 to 2 of 2

Thread: Embed VB.NET or C# code in a thinBasic script

  1. #1
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    52
    Rep Power
    15

    Embed VB.NET or C# code in a thinBasic script

    Using PSCom,
    you can embed VB.NET or C# code in a thinBasic script, and execute it.

    uses "console"
    
    dim ps as iDispatch
    dim result as string
    Dim VBNETCode as string 
    
    ps = CreateObject("PSCom.PSScript")
    
    if IsComObject(ps) Then
    
      VBNETCode = "$vbnetCode = @" + CHR$(34) + vbCRLF
      VBNETCode = VBNETCode + "Imports System" + vbCRLF
      VBNETCode = VBNETCode + "Public Class HelloWorld" + vbCRLF
      VBNETCode = VBNETCode + "  Public Function GetMessage() As String" + vbCRLF
      VBNETCode = VBNETCode + "    Return " + chr$(34) + "Hello from VB.NET!" + chr$(34) + vbCRLF
      VBNETCode = VBNETCode + "  End Function" + vbCRLF
      VBNETCode = VBNETCode + "End Class" + vbCRLF
      VBNETCode = VBNETCode + Chr$(34) + "@" + vbCRLF
      VBNETCode = VBNETCode + "Add-Type -TypeDefinition $vbnetCode -Language VisualBasic" + VBCRLF
      VBNETCode = VBNETCode + "$helloWorld = New-Object HelloWorld" + VBCRLF
      VBNETCode = VBNETCode + "$message = $helloWorld.GetMessage()" + VBCRLF
      VBNETCode = VBNETCode + "Write-Output $message" + VBCRLF
    
      result = ps.ExecuteCommand(VBNETCode)
    
      printl result
    EndIf
    
    ps = Nothing
    
    Joe

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,159
    Rep Power
    736
    Thanks again for sharing an interesting solution.

    Little tip to take advantage of thinBASIC string specification over multiple lines.

    Instead of this:
     VBNETCode = "$vbnetCode = @" + CHR$(34) + vbCRLF
     VBNETCode = VBNETCode + "Imports System" + vbCRLF
     VBNETCode = VBNETCode + "Public Class HelloWorld" + vbCRLF
     VBNETCode = VBNETCode + "  Public Function GetMessage() As String" + vbCRLF
     VBNETCode = VBNETCode + "    Return " + chr$(34) + "Hello from VB.NET!" + chr$(34) + vbCRLF
     VBNETCode = VBNETCode + "  End Function" + vbCRLF
     VBNETCode = VBNETCode + "End Class" + vbCRLF
     VBNETCode = VBNETCode + Chr$(34) + "@" + vbCRLF
     VBNETCode = VBNETCode + "Add-Type -TypeDefinition $vbnetCode -Language VisualBasic" + VBCRLF
     VBNETCode = VBNETCode + "$helloWorld = New-Object HelloWorld" + VBCRLF
     VBNETCode = VBNETCode + "$message = $helloWorld.GetMessage()" + VBCRLF
     VBNETCode = VBNETCode + "Write-Output $message" + VBCRLF
    
    ...you can do this (difference is in adding + sign at the end of the line):
    VBNETCode = "$vbnetCode = @" + CHR$(34) + vbCRLF +
                "Imports System" + vbCRLF +
                "Public Class HelloWorld" + vbCRLF +
                "  Public Function GetMessage() As String" + vbCRLF +
                "    Return " + chr$(34) + "Hello from VB.NET!" + chr$(34) + vbCRLF +
                "  End Function" + vbCRLF +
                "End Class" + vbCRLF +
                Chr$(34) + "@" + vbCRLF +
                "Add-Type -TypeDefinition $vbnetCode -Language VisualBasic" + VBCRLF +
                "$helloWorld = New-Object HelloWorld" + VBCRLF +
                "$message = $helloWorld.GetMessage()" + VBCRLF +
                "Write-Output $message" + VBCRLF
    

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Embed thinBasic as scripting language to visual studio
    By ReneMiner in forum thinBasic General
    Replies: 2
    Last Post: 25-06-2020, 17:50
  2. Is there a thinBASIC Script UI Designer
    By gian20 in forum thinBasic General
    Replies: 2
    Last Post: 09-04-2012, 04:06
  3. DLL ready so Thinbasic can be used as a script language?
    By Michael Hartlef in forum thinBasic General
    Replies: 8
    Last Post: 29-02-2008, 12:03
  4. thinBasic as script
    By kryton9 in forum thinBasic General
    Replies: 23
    Last Post: 18-12-2006, 14:52

Members who have read this thread: 3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •