PDA

View Full Version : Using a .NET Framework dll with thinBasic



Joe Caverly
08-04-2025, 20:56
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

ErosOlmi
09-04-2025, 07:28
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

Joe Caverly
09-04-2025, 14:48
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 (https://www.thinbasic.com/community/showthread.php?13383-Execute-a-PowerShell-ps1-script-or-a-PowerShell-command(s)&p=96993&viewfull=1#post96993) 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