help converting function from vb6 to gambas

Tiberius
Posts: 5
Joined: Monday 25th May 2020 6:33pm

help converting function from vb6 to gambas

Post by Tiberius » Monday 25th May 2020 7:18pm

Hello, Im very new to Gambas3. I used to write simple VB6 as a hobby some time ago. I want to switch to Gambas.

Can someone help me convert a function a wrote several years ago from VB6 to Gambas3?
'Random letters in a string
Public Function X(ByVal sdata As String)
Dim OldWord As String
Dim NewWord As String
Dim temp As String
OldWord = sdata
While Not OldWord = vbNullString
Dim RandomPosition As Integer = StrRnd(Len(OldWord))
Dim RandomLetter As String = Mid(OldWord, RandomPosition, 1)
NewWord = NewWord + RandomLetter
For i = 1 To RandomPosition - 1
temp = temp + Mid(OldWord, i, 1)
Next
For j = RandomPosition + 1 To Len(OldWord)
temp = temp + Mid(OldWord, j, 1)
Next
OldWord = temp
temp = Nothing
End While
If Not NewWord = sdata Then
X = NewWord
Else
X(NewWord)
End If
End Function
Function StrRnd(ByVal i)
Dim x As Integer
Randomize()
StrRnd = Int((Val(i) * Rnd()) + 1)
End Function

User avatar
grayghost4
Posts: 68
Joined: Wednesday 05th December 2018 5:00am
Location: Concord, CA usa

Re: help converting function from vb6 to gambas

Post by grayghost4 » Monday 25th May 2020 8:38pm

perhaps a little reading will help :

http://wordpress.gambas.one/

several good starter books

If you have an ereader ... this is a good book

https://www.amazon.com/Beginners-Guide- ... =8-1-fkmr0

Tiberius
Posts: 5
Joined: Monday 25th May 2020 6:33pm

Re: help converting function from vb6 to gambas

Post by Tiberius » Monday 25th May 2020 9:08pm

thanks for the quick reply i will look into it.

User avatar
grayghost4
Posts: 68
Joined: Wednesday 05th December 2018 5:00am
Location: Concord, CA usa

Re: help converting function from vb6 to gambas

Post by grayghost4 » Monday 25th May 2020 9:33pm

as you move through the program it will give the syntax for each function

Image

User avatar
Godzilla
Posts: 46
Joined: Wednesday 26th September 2018 11:20am

Re: help converting function from vb6 to gambas

Post by Godzilla » Monday 25th May 2020 10:20pm

Tiberius, welcome to Gambas. I also came from enjoying VB6 for years, and missing it terribly. Gambas is a godsend.

I played around with your functions and i got them to work. I had to take some liberties to change things around a bit. For example, "Temp" is already a built-in function in Gambas. And "X" for the name of a function is a bit too general, in my opinion. Anyways, on to the code:
Public Function strX(sdata As String) As String

  Dim OldWord As String
  Dim NewWord As String
  Dim TempString As String
  Dim RandomLetter As String
  
  Dim RandomPosition As Integer  
  Dim i As Integer
  Dim j As Integer
  
  OldWord = sdata
  
  Do While OldWord <> Null
    RandomPosition = StrRnd(Len(OldWord))
    RandomLetter = Mid(OldWord, RandomPosition, 1)
    NewWord = NewWord & RandomLetter
    For i = 1 To RandomPosition - 1
      TempString = TempString & Mid(OldWord, i, 1)
    Next
    For j = RandomPosition + 1 To Len(OldWord)
      TempString = TempString & Mid(OldWord, j, 1)
    Next
    OldWord = TempString
    TempString = Null
  Loop
  If NewWord <> sdata Then
    Return NewWord
  Else
    Return Null
  Endif
  
End
  
Public Function StrRnd(i As Integer) As Integer

 Dim x As Integer
 Randomize
 Return Int((Rnd * i) + 1)
 
End
A snafu I noticed in your original code is that the first function appears to call itself again, from within itself, if "NewWord" is blank. I changed this to be handled by putting the call to the first function into a "Do" loop like this:
Do
   TextBox1.Text = strX("gambas is basic")
   Wait 0.1
Loop Until TextBox1.Text.Len > 0
Also, the integer "x" in the second function doesn't appear to be used for anything. In any case, the results I got from calling the first function 5 times are:

Code: Select all

sba mcgaba isis
masabaibc gss i
aba sbssmiai gc
 s abiabmsiasgc
 baasbsi siamgc
I hope I didn't change things so much that this output isn't what you were expecting. I hope this helps you.

Tiberius
Posts: 5
Joined: Monday 25th May 2020 6:33pm

Re: help converting function from vb6 to gambas

Post by Tiberius » Tuesday 26th May 2020 4:56pm

thank you very much I used VB6 since school then highschool and i miss it a lot when they dumped it in favour of .NET. Years have passed and i switched to linux . My current job dosen't require programming and i lost contact with coding for years. Because of the big quarantine i have spare time and i wanted to convert some code to Gambas3 . ( i have read about it on the internet and it looked promissing for me).

I will check your code and analyze differences. I also got the book grayghost recommended me and i will start reading it.

PS1: Sorry for any mistakes , english is not my native language.
PS2: I had some troubles installing gambas3 on ubuntu but i figured it out , if it helps someone on basic ubuntu 20.04 it fails to start because it needs "gambas3-gb-form-print" ; a simple sudo apt install gambas3-gb-form-print will fix any problem

cage
Posts: 76
Joined: Monday 02nd September 2019 5:47am
Location: Phoenix Arizona

Re: help converting function from vb6 to gambas

Post by cage » Tuesday 26th May 2020 6:32pm

PS2: I had some troubles installing gambas3 on ubuntu but i figured it out , if it helps someone on basic ubuntu 20.04 it fails to start because it needs "gambas3-gb-form-print" ; a simple sudo apt install gambas3-gb-form-print will fix any problem
If you used Discover to install Gambas, your missing half of the Gambas libraries (including gambas3-gb-form-print). I found this out when installing Gambas for the first time on Kubuntu. To do a complete install use Synaptic or from the command line use sudo apt install gambas3. It will save you a lot of head aches in the future.

Tiberius
Posts: 5
Joined: Monday 25th May 2020 6:33pm

Re: help converting function from vb6 to gambas

Post by Tiberius » Thursday 28th May 2020 10:55pm

Im using Ubuntu 20.04. I installed gambas3 with sudo apt install gambas3. I found that (the hard way) that it dosen't install gambas3-gb-form-prin so gambas3 fails to start.

PS:
Im playing with text files. I want to write a simple app that reads a .srt (subtitle file) then writes the same file with UTF8 encoding. For example i want to read the "invisible.srt" wich has character encoding WESTERN (ISO-8859-15) and write invisible2.srt with UTF8.

I know i can use Conv$ to convert from one encoding to another but..is there any way or function to convert from ANY encoding to UFT8? My app will need to eventualy convert all .srt files (wich may have different encodings) from one folder to UFT8

Or is there any GetEncoding posibility?

Dim xFile As File
Dim sLine As String

Dim yFile As File


xFile = Open Application.Path & "/" & "invisible.srt" For Input
yFile = Open Application.Path & "/" & "invisible2.srt" For Write Create

While Not Eof(xFile)
Line Input #xFile, sLine

Write #yFile, sLine & gb.NewLine
Wend

Close #xFile
Close #yFile

User avatar
cogier
Site Admin
Posts: 508
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: help converting function from vb6 to gambas

Post by cogier » Friday 29th May 2020 3:29pm

The art of working out what the character set of a file requires a bit of guess works I am told. However there is a nice command line tool that seems to do this very well. It's called 'uchardet'. You will probably need to install this.

Consider the following code: -
Public Sub Form_Open()

  ConvertFile(User.Home &/ "search-orig.csv", User.Home &/ "search-orig1.csv")  'Call the subroutine with old and new file paths

End

Public Sub ConvertFile(sFileName As String, sNewFileName As String)

  Dim sChar As String                                                           'To hold the 'Character set'
  Dim sFile As String = File.Load(sFileName)                                    'Load the file into a string

  Shell "uchardet " & sFileName To sChar                                        'Get 'uchardet' to tell us the 'Character set'

  sChar = Replace(sChar, gb.NewLine, "")                                        'Get rid of the new line character 'uchardet' returns at the end of the line

  If sChar <> "UTF-8" Then                                                      'If the file is not UTF-8 then..
    sFile = Conv(sFile, sChar, "UTF-8")                                         'Convert the file from whatever 'uchardet' found to UTF-8
    File.Save(sNewFileName, sFile)                                              'Save the file with the new name
  End If

End


Note that this is unnecessary xFile = Open Application.Path & "/" & "invisible.srt" For Input
Use &/ it works out if you need (or don't) a '/'

Both lines below will work: -
Open Application.Path &/ "invisible.srt" For Input
Open Application.Path &/ "/invisible.srt" For Input

cage
Posts: 76
Joined: Monday 02nd September 2019 5:47am
Location: Phoenix Arizona

Re: help converting function from vb6 to gambas

Post by cage » Saturday 30th May 2020 4:55am

Tiberius check out the project showcase portion. There is a program I wrote which is a simple filing cabinet program. I included a collection of examples from Gambas, some from me and some from others. It might give you some answers to your questions. Welcome to the Gambas world and to Gambas One.

Post Reply