A Dirty Guide to OOP #1

Post your Gambas programming questions here.
salihburhan
Posts: 15
Joined: Thursday 24th October 2019 2:14pm

Re: A Dirty Guide to OOP #1

Post 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..
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: A Dirty Guide to OOP #1

Post 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?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: A Dirty Guide to OOP #1

Post 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.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: A Dirty Guide to OOP #1

Post 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.
.... and carry a big stick!
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: A Dirty Guide to OOP #1

Post 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?
salihburhan
Posts: 15
Joined: Thursday 24th October 2019 2:14pm

Re: A Dirty Guide to OOP #1

Post 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?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: A Dirty Guide to OOP #1

Post 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 441 times
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A Dirty Guide to OOP #1

Post 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.
If at first you don't succeed , try doing something differently.
BruceS
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

Re: A Dirty Guide to OOP #1

Post 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
Post Reply