help converting function from vb6 to gambas

Post

Posted
Rating:
#1 (In Topic #419)
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
thanks for the quick reply i will look into it.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
as you move through the program it will give the syntax for each function

<IMG src="https://i.imgur.com/FABVT6O.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
Godzilla is in the usergroup ‘Regular’
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:

Code (gambas)

  1.  
  2.   Dim OldWord As String
  3.   Dim NewWord As String
  4.   Dim TempString As String
  5.   Dim RandomLetter As String
  6.  
  7.   Dim RandomPosition As Integer  
  8.  
  9.   OldWord = sdata
  10.  
  11.   Do While OldWord <> Null
  12.     RandomPosition = StrRnd(Len(OldWord))
  13.     RandomLetter = Mid(OldWord, RandomPosition, 1)
  14.     NewWord = NewWord & RandomLetter
  15.     For i = 1 To RandomPosition - 1
  16.       TempString = TempString & Mid(OldWord, i, 1)
  17.     Next
  18.     For j = RandomPosition + 1 To Len(OldWord)
  19.       TempString = TempString & Mid(OldWord, j, 1)
  20.     Next
  21.     OldWord = TempString
  22.     TempString = Null
  23.   Loop
  24.   If NewWord <> sdata Then
  25.     Return NewWord
  26.   Else
  27.     Return Null
  28.  
  29.  
  30.  
  31.  Return Int((Rnd * i) + 1)
  32.  

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:

Code (gambas)

  1.    TextBox1.Text = strX("gambas is basic")
  2.    Wait 0.1
  3. 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

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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
cage is in the usergroup ‘Regular’
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.
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Guru
cogier is in the usergroup ‘Guru’
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: -

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   ConvertFile(User.Home &/ "search-orig.csv", User.Home &/ "search-orig1.csv")  'Call the subroutine with old and new file paths
  4.  
  5.  
  6. Public Sub ConvertFile(sFileName As String, sNewFileName As String)
  7.  
  8.   Dim sChar As String                                                           'To hold the 'Character set'
  9.   Dim sFile As String = File.Load(sFileName)                                    'Load the file into a string
  10.  
  11.   Shell "uchardet " & sFileName To sChar                                        'Get 'uchardet' to tell us the 'Character set'
  12.  
  13.   sChar = Replace(sChar, gb.NewLine, "")                                        'Get rid of the new line character 'uchardet' returns at the end of the line
  14.  
  15.   If sChar <> "UTF-8" Then                                                      'If the file is not UTF-8 then..
  16.     sFile = Conv(sFile, sChar, "UTF-8")                                         'Convert the file from whatever 'uchardet' found to UTF-8
  17.     File.Save(sNewFileName, sFile)                                              'Save the file with the new name
  18.   End If
  19.  
  20.  
 
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
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Regular
cage is in the usergroup ‘Regular’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#11
Trainee
 thanks , i'll take a look
Online now: No Back to the top
1 guest and 0 members have just viewed this.