User Define Types

Post your Gambas programming questions here.
User avatar
sarpomira
Posts: 9
Joined: Monday 28th December 2020 4:15pm

User Define Types

Post by sarpomira » Wednesday 06th January 2021 3:25am

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

User avatar
Quincunxian
Posts: 112
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: User Define Types

Post by Quincunxian » Wednesday 06th January 2021 4:04am

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.
Cheers - Quin.
I code therefore I am

User avatar
sarpomira
Posts: 9
Joined: Monday 28th December 2020 4:15pm

Re: User Define Types

Post by sarpomira » Thursday 07th January 2021 4:42pm

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 1007 times

User avatar
stevedee
Posts: 405
Joined: Monday 20th March 2017 6:06pm

Re: User Define Types

Post by stevedee » Thursday 07th January 2021 5:28pm

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!

User avatar
cogier
Site Admin
Posts: 674
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: User Define Types

Post by cogier » Thursday 07th January 2021 5:47pm

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 55 times

vuott
Posts: 69
Joined: Wednesday 05th April 2017 6:07pm
Location: European Union

Re: User Define Types

Post by vuott » Thursday 07th January 2021 6:34pm


User avatar
sarpomira
Posts: 9
Joined: Monday 28th December 2020 4:15pm

Re: User Define Types

Post by sarpomira » Thursday 07th January 2021 6:54pm

Once again, thank you gentlemen.
Much appreciated

User avatar
BruceSteers
Posts: 443
Joined: Thursday 23rd July 2020 5:20pm

Re: User Define Types

Post by BruceSteers » Friday 08th January 2021 4:50pm

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
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
sarpomira
Posts: 9
Joined: Monday 28th December 2020 4:15pm

Re: User Define Types

Post by sarpomira » Wednesday 13th January 2021 11:36pm

Thanks BruceSteers,
Tried your code.
Still no luck. :(

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

User avatar
PJBlack
Posts: 121
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: User Define Types

Post by PJBlack » Thursday 14th January 2021 1:54am

Attachments
Bildschirmfoto_2021-01-14_02-51-15.png
Bildschirmfoto_2021-01-14_02-51-15.png (75.87 KiB) Viewed 954 times

Post Reply