Page 2 of 2

Re: A Dirty Guide to OOP #1

Posted: Wednesday 26th February 2020 11:41am
by salihburhan
I was wondering if it is at all possible to create objects at runtime.
Say, I have a resultset with two fields: id, oName
Can I create a class?

Public id as integer
public oName as string

public sub set_delete() --go delete
public sub set_by_id($id as integer) -- go populate obj
public sub set_insert() -- go insert
etc..

Re: A Dirty Guide to OOP #1

Posted: Wednesday 26th February 2020 4:33pm
by cogier
I was wondering if it is at all possible to create objects at runtime.
Run this in a new 'Graphical application'
Label1 As Label
Button1 As Button

Public Sub Form_Open()

  With Label1 = New Label(Me)
    .Text = "Hello salihburhan"
    .Font.Size = 25
    .Font.Bold = True
    .Width = 300
    .Height = 56
    .X = 50
    .Y = 10
  End With

  With Button1 = New Button(Me) As "Button1"
    .Text = "&Click me!"
    .Height = 28
    .Width = 98
    .Picture = Picture["icon:/22/apply"]
    .X = 50
    .Y = 70
  End With

End

Public Sub Button1_Click()

  If Label1.Text = "Hello salihburhan" Then
    Label1.Text = "From cogier"
  Else
    Label1.Text = "Hello salihburhan"
  Endif

End
Grab the code from here and run it in the same way to see what can be done.

Does that help?

Re: A Dirty Guide to OOP #1

Posted: Wednesday 26th February 2020 8:32pm
by stevedee
salihburhan wrote: Wednesday 26th February 2020 11:41am ...Can I create a class?...
Cogier's example shows creating objects from classes, but I don't believe that it is possible to create a class at runtime, if that is what you are asking.

Languages like VB.Net have a CreateClass command, but I don't think Gambas has an equivalent.

Re: A Dirty Guide to OOP #1

Posted: Monday 2nd March 2020 6:17pm
by Cedron
Just for the sake of argument, I am going to disagree with you.

Just funning.

Writing code generators is fairly straightforward. Given a list of fields and their types, there is certainly a formula for codifying that as a Gambas Class file.

Thus, one could write a program that reads the specs for class definition, writes the class definition as source code (along with a testing program), compiles the code, then shells out to run the compiled code.

Technically, it's the same thread executing the code it generated.

Re: A Dirty Guide to OOP #1

Posted: Tuesday 3rd March 2020 10:17am
by stevedee
Cedron wrote: Monday 2nd March 2020 6:17pm ...Writing code generators is fairly straightforward. Given a list of fields and their types, there is certainly a formula for codifying that as a Gambas Class file....
Hey that sounds interesting. Can you give us a simple code example?

Re: A Dirty Guide to OOP #1

Posted: Tuesday 3rd March 2020 5:27pm
by salihburhan
If there is a compiler engine of sorts like a class that can be imported and called at runtime for specific subroutines....
So maybe it might be possible to pass along a string to be compiled and a name for the class.
It can return a variable that has standard methods.
$var.insert()
$var.update_name()

Other than databases that host frameworks (like wordpress), would there be any other application?

Re: A Dirty Guide to OOP #1

Posted: Wednesday 4th March 2020 4:45pm
by cogier
Hey that sounds interesting. Can you give us a simple code example?
This got me thinking and I came up with the attached program. This program will create a new folder and all the normal Gambas files and folders. It will add the code to FMain.class and then compile, make an executable and run it, all nearly instantly. You can add some text to the new program which will be displayed at the bottom of the Form.

I'm not sure this is exactly what people were looking for but here it is anyway.

Image
CreateCode.tar.gz
(45.05 KiB) Downloaded 452 times

Re: A Dirty Guide to OOP #1

Posted: Friday 5th February 2021 2:39am
by BruceSteers
Just an additional note on setting up the Properties..

As of Gambas 3.14 the "Use" keyword was introduced to save on code.

So before you would write..

Property MyProperty As String  ' define property

Private $MyProperty As String  ' define private variable

' define read and write methods

Private Function MyProperty_Read() As String

  Return $MyProperty

End

Private Sub MyProperty_Write(Value As String)

  $MyProperty = Value

End


Now you can simply write...

Property MyProperty As String Use $MyProperty

This saves a lot of code but take note..
It will make your application fail if run on a gambas version less than 3.14 so if you want to share your app with debian users who might still be using Gambas 3.12 then do not use it.

Re: A Dirty Guide to OOP #1

Posted: Sunday 7th February 2021 7:06pm
by 01McAc
A very good idea and ideal for beginners like me, thank you very much. The guide is supposed to be renamed to A Fruity Guide to OOP :D