User Define Types

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

User Define Types

Post 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
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: User Define Types

Post 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.
Cheers - Quin.
I code therefore I am
User avatar
sarpomira
Posts: 20
Joined: Monday 28th December 2020 4:15pm

Re: User Define Types

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

Re: User Define Types

Post by stevedee »

sarpomira wrote: Thursday 7th 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: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: User Define Types

Post 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 299 times
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: User Define Types

Post by vuott »

Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
sarpomira
Posts: 20
Joined: Monday 28th December 2020 4:15pm

Re: User Define Types

Post by sarpomira »

Once again, thank you gentlemen.
Much appreciated
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: User Define Types

Post 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
If at first you don't succeed , try doing something differently.
BruceS
User avatar
sarpomira
Posts: 20
Joined: Monday 28th December 2020 4:15pm

Re: User Define Types

Post 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 ?
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: User Define Types

Post by PJBlack »

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