Page 1 of 2

User Define Types

Posted: Wednesday 06th January 2021 3:25am
by sarpomira
Hello,

In VB6 there were "User Defined Types" available.
Does Gambas have a similar construct?
I am unable to get the traditional VB syntax to work

Example of the VB syntax for creating a User Defined Type:

Type Employee As String
FirstName As String
LastName As String
DateOfHire As Date
Salary As Currency
EmployeeNumber As Integer
End Type

Public EmployeeData as Employee



Thanks kindly

Re: User Define Types

Posted: Wednesday 06th January 2021 4:04am
by Quincunxian
Gambas uses an object called a STRUCT
Public Struct Employee
  EmployeeNo As String
  FirstName As String
  LastName As String
  DateOfHire As Date
  Salary As Float
  EmployeeNumber As Integer
End Struct
There are some overheads: please read this carefully on the Gambas Wiki on the STRUCT

The difference between a class and STRUCT is minimal so I stopped using a STRUCT quite a while ago.
There is a huge of benefit in using a Class as you can abstract all the DB interfaces and any other routines into one place.

Re: User Define Types

Posted: Thursday 07th January 2021 4:42pm
by sarpomira
Thanks for the reply.
I keep hitting an error when setting the variable values (strings).
Gambas is requesting an "AS" in the definition. Any idea what could be causing this?

Code: Select all

Public Struct Employee
  EmployeeNo As String
  FirstName As String
  LastName As String
End Struct


Public EmployeeRecord As Employee

EmployeeRecord.EmployeeNo = "22"
EmployeeRecord.FirstName = "Roger"
EmployeeRecord.LastName = "Blake"



Public Sub cmdSelect_Click()
    
TextBox1.text = EmployeeRecord.FirstName
  
End

Public Sub Form_Open()

End
This picture shows the error when the program is run.
Error.png
Error.png (48.83 KiB) Viewed 1325 times

Re: User Define Types

Posted: Thursday 07th January 2021 5:28pm
by stevedee
sarpomira wrote:
Thursday 07th January 2021 4:42pm
...I keep hitting an error when setting the variable values (strings).
Gambas is requesting an "AS" in the definition. Any idea what could be causing this?

Code: Select all

Public Struct Employee
  EmployeeNo As String
  FirstName As String
  LastName As String
End Struct
I think the example given may be wrong as the help page says:-

Code: Select all

[ PRIVATE | PUBLIC ] Identifier AS STRUCT Structure name
...so try adding AS before Struct:-
Public AS Struct Employee
  EmployeeNo As String
  FirstName As String
  LastName As String
End Struct
Also, turn on Line Numbers in Editor Preferences as: Missing AS in FMain.class:12 is referring to the line number
...although it doesn't always help!

Re: User Define Types

Posted: Thursday 07th January 2021 5:47pm
by cogier
As Quincunxian originally said, I think you would be a lot better off using a Class.

I have written a small program that may be of interest. The Employee Class can have as many extra variables added as you see fit (Age, Address, Gender etc.)
Test2-0.0.3.tar.gz
(13.21 KiB) Downloaded 69 times

Re: User Define Types

Posted: Thursday 07th January 2021 6:34pm
by vuott

Re: User Define Types

Posted: Thursday 07th January 2021 6:54pm
by sarpomira
Once again, thank you gentlemen.
Much appreciated

Re: User Define Types

Posted: Friday 08th January 2021 4:50pm
by BruceSteers
The assignments for EmployeeRecord are not in a sub/function they are in the global space. that can't be right.
Maybe try assigning the values in the Form_Open() Sub
you also may need to initialise the pointer first with 'New' ...
Ie...
EmployeeRecord = New Employee

Public Struct Employee
  EmployeeNo As String
  FirstName As String
  LastName As String
End Struct


Public EmployeeRecord As Employee


Public Sub cmdSelect_Click()
    
TextBox1.text = EmployeeRecord.FirstName
  
End

Public Sub Form_Open()

EmployeeRecord.EmployeeNo = "22"
EmployeeRecord.FirstName = "Roger"
EmployeeRecord.LastName = "Blake"


End

Re: User Define Types

Posted: Wednesday 13th January 2021 11:36pm
by sarpomira
Thanks BruceSteers,
Tried your code.
Still no luck. :(

I wonder is "Struct" has a dependency to a Component added from the component library ?

Re: User Define Types

Posted: Thursday 14th January 2021 1:54am
by PJBlack