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 Define Types
- Quincunxian
- Posts: 173
- Joined: Sunday 25th June 2017 12:14am
- Location: Western Australia
Re: User Define Types
Gambas uses an object called a 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.
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 STRUCTThe 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
I code therefore I am
Re: User Define Types
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?
This picture shows the error when the program is run.
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
Re: User Define Types
I think the example given may be wrong as the help page says:-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
Code: Select all
[ PRIVATE | PUBLIC ] Identifier AS STRUCT Structure name
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!
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: User Define Types
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.)
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.)
Re: User Define Types
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
Re: User Define Types
Once again, thank you gentlemen.
Much appreciated
Much appreciated
- BruceSteers
- Posts: 1569
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: User Define Types
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
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
BruceS
Re: User Define Types
Thanks BruceSteers,
Tried your code.
Still no luck.
I wonder is "Struct" has a dependency to a Component added from the component library ?
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
- Attachments
-
- Bildschirmfoto_2021-01-14_02-51-15.png (75.87 KiB) Viewed 6562 times