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..
A Dirty Guide to OOP #1
- cogier
- Site Admin
- Posts: 1160
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: A Dirty Guide to OOP #1
Run this in a new 'Graphical application'I was wondering if it is at all possible to create objects at runtime.
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
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
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.
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.
.... and carry a big stick!
-
- Posts: 15
- Joined: Thursday 24th October 2019 2:14pm
Re: A Dirty Guide to OOP #1
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?
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?
- cogier
- Site Admin
- Posts: 1160
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: A Dirty Guide to OOP #1
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.Hey that sounds interesting. Can you give us a simple code example?
I'm not sure this is exactly what people were looking for but here it is anyway.
- BruceSteers
- Posts: 1874
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: A Dirty Guide to OOP #1
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..
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.
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.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: A Dirty Guide to OOP #1
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