Results 1 to 3 of 3

Thread: Using a .NET Framework dll with thinBasic

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

    Using a .NET Framework dll with thinBasic

    Not many people realize that you can use .NET Framework dlls with compilers such as PowerBasic.

    I have translated a PowerBasic program,
    which I wrote back in 2014,
    to work with thinBasic.

    When I run the following code,
    Hebrew.tbasicc,
    using thinbasicc.exe,
    it returns
    Nissan 10, 5785
    which is the same as
    April 8, 2025.

    '---Load Console Module
    Uses "Console"
    
    dim Hebrew as IDispatch
    dim TheYear as String
    Dim TheMonth as string
    Dim TheDay as string
    dim LeapYear as string
    Dim HebrewDate as string
    
    Hebrew = CreateObject("System.Globalization.HebrewCalendar")
    
    TheYear = Str$(Hebrew.GetYear(Date$(0)))
    
    If Hebrew.IsLeapYear(TheYear) Then
      LeapYear = "Y"
    else
      LeapYear = "N"
    end if
    
    TheDay = Str$(Hebrew.GetDayOfMonth(Date$(0)))
    TheDay = TrimFull$(TheDay)
    
    TheMonth = Hebrew.GetMonth(Date$(0))
    TheMonth = TrimFull$(TheMonth)
    
    Select Case Val(TheMonth)
      Case 1
        TheMonth = "Tishrei"
      Case 2
        TheMonth = "Cheshvan"
      Case 3
        TheMonth = "Kislev"
      Case 4
        TheMonth = "Tevet"
      Case 5
        TheMonth = "Shevat"
      Case 6
        If LeapYear = "Y" Then
          TheMonth = "Adar Alef"
        Else
          TheMonth = "Adar"
        End If
      Case 7
        If LeapYear ="Y" Then
          TheMonth = "Adar Beit"
        Else
          TheMonth = "Nissan"
        End If
      Case 8
        If LeapYear = "Y" Then
          TheMonth = "Nissan"
        Else
           TheMonth = "Iyar"
        End If
      Case 9
        If LeapYear "Y" Then
          TheMonth = "Iyar"
        Else
          TheMonth = "Sivan"
        End If
      Case 10
        If LeapYear "Y" Then
          TheMonth = "Sivan"
        Else
           TheMonth = "Tamuz"
        End If
      Case 11
        If LeapYear = "Y" Then
          TheMonth = "Tamuz"
        Else
          TheMonth = "Av"
        End If
      Case 12
        If LeapYear = "Y" Then
          TheMonth = "Av"
        Else
          TheMonth = "Elul"
        End If
      Case 13
          TheMonth = "Elul"
      '
      ' This should never be executed
      '
      Case Else
        TheMonth = CStr(Hebrew.GetMonth(Date$(0)))
    End Select
    
    TheMonth = TrimFull$(TheMonth)
    
    HebrewDate = TheMonth + " " + TheDay + "," + TheYear
    
    Hebrew = Nothing 
    
    PrintL HebrewDate
    PrintL
    
    You can use any .NET language,
    such as C# or VB.NET,
    to create your own .NET Framework dll for use with thinBasic.

    VB.NET (or C#) might also work with thinBasic via External Compiler Interop,
    just like FreeBASIC or PowerBASIC.

    Joe

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    58
    Posts
    8,834
    Rep Power
    10
    Thanks a lot Joe for this example!

    In general thinBasic CreateObject or NewCom commands can create objects from .Net classes if classes are not static, exposes a COM interface and do not require any parameter when instantiating the class into an object at runtime.
    So there are many .Net classes that can be created and used in thinBasic just out of the box using CreateObject or NewCom
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    52
    Rep Power
    15
    Many people may not want to create a COM dll using .NET Framework,
    especially if it is just a small job, not worthy of being created as a COM dll.

    In that case, using PSCom from thinBasic may be a better solution when VB.NET or C# code is needed.

    Here's thinBasic code to create a VB.NET object, and call a VB.NET method from thinBasic.

    uses "console"
    
    dim ps as iDispatch
    dim result as string
    
    ps = CreateObject("PSCom.PSScript")
    
    if IsComObject(ps) Then
      result = ps.ExecuteScript("e:\utils\embedvbnet.ps1")
      
      printl result
    EndIf
    
    ps = Nothing
    
    Here's the PowerShell code (embedvbnet.ps1) to create the VB.NET object.

    # Define the VB.NET code as a string
    $vbnetCode = @"
    Imports System
    
    Public Class HelloWorld
        Public Function GetMessage() As String
            Return "Hello from VB.NET!"
        End Function
    End Class
    "@
    
    # Use Add-Type to compile the VB.NET code
    Add-Type -TypeDefinition $vbnetCode -Language VisualBasic
    
    # Create an instance of the VB.NET class
    $helloWorld = New-Object HelloWorld
    
    # Call the method and display the message
    $message = $helloWorld.GetMessage()
    Write-Output $message
    
    Running the thinBasic code from the console results in Hello from VB.NET being displayed.

    Joe

Similar Threads

  1. OOP Framework - reveals oxygen bug -
    By efgee in forum Programs
    Replies: 13
    Last Post: 19-09-2010, 16:58
  2. Visual Studio 2010 and .NET Framework 4.0 Overview
    By ErosOlmi in forum Development
    Replies: 3
    Last Post: 02-10-2008, 16:23

Members who have read this thread: 6

Posting Permissions

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