# A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/User-defined data types

You have already met a variety of built-in datatypes with integers, strings, chars and more. But often these limited datatypes aren't enough and a programmer wants to build their own datatypes. Just as an integer is restricted to "a whole number from -2,147,483,648 through 2,147,483,647", user-defined datatypes have limits placed on their use by the programmer.
Enumerated, subrange, sets

## Enumerated

If you are using lots of constants in your program that are all related to each other it is a good idea to keep them together using a structure called an Enum. For example you might want to store the names of each set of cards in a deck, instead of writing:
We can bring them together in a nice neat structure called an enum:
This allows you to set meaningful names to the enum and its members, meaning it it easier to remember and makes your code more readable.
We might also create separate constants to store the points of football results
With enums we can create a datatype called Result and store the points within it, under easy to remember name:
* Exercise: Enumerated types
* Declare an enum called months that holds the months along with how many days in each for a non-leap year
* Answer:
* Enum months
* January = 31
* February = 28
* March = 31
* April = 30
* May = 31
* June = 30
* July = 31
* August = 31
* September = 30
* October = 31
* November = 30
* December = 31
* End Enum
* What would the following code do:
* Enum DayOfWeek
* Monday = 1
* Tuesday = 2
* Wednesday = 3
* Thursday = 4
* Friday = 5
* Saturday = 6
* Sunday = 7
* End Enum
* Answer:
* Code Output

## Records and Fields

Records are collections of data items (fields) stored about something. They allow you to combine several data items (or fields) into one variable. An example at your college they will have a database storing a record for each student. This student record would contain fields such as ID, Name and Date of Birth. The following example that DOESN'T USE RECORDS might be simple enough for one student:
For the following input:
insert the id: 12 insert the name: Nigel insert the Date of Birth: 12/12/1994 new record created: 12 Nigel 12/12/1994
But what if your college has more than one student, we'd have to write:
It would take an awfully long time to declare them all, let alone saving writing data to them. So how do we solve this? Well we need to combine two things we have learnt about so far, the record and the array. We are going to make an array of student records:
This seems to solve our problem, you might want to try it out yourself but decrease the number of students slightly!
* Exercise: Records
* Declare an record called player to store the following Role Playing Game attributes: health, name, characterclass (barbarian, wizard, elf), gold, gender
* Answer:
* Enum type
* WIZARD
* BARBARIAN
* ELF
* End Enum
* Structure player 'remember Visual Basic uses structure instead of record
* name as string
* health as integer
* gold as integer
* gender as char
* ' class as string ' you might have already noticed, the word class is 'reserved'
* ' this means it is has a special purpose in VBNET and we'll have to use another
* characterclass as type 'a string would work, but it's better to use an enum
* End player
* Creates 2 characters, Gandolf and Conan using the player record
* Answer:
* 'you can of course give them different attributes
* Dim Gandolf As player
* Gandolf.name = "Gandolf"
* Gandolf.health = 70
* Gandolf.gold = 50
* Gandolf.gender = "m"
* Gandolf.characterclass = type.WIZARD
* Dim Conan As player
* Conan.name = "Conan"
* Conan.health = 100
* Conan.gold = 30
* Conan.gender = "m"
* Conan.characterclass = type.BARBARIAN
However, what use is a program that only saves all the students for as long as it is running? We need to know how to write to files, and read the data back, we'll look at that in the next chapter.
* Extension: Object Orientation
* It's pretty cool creating your own datatypes where you can set up collections of attributes (fields), but it would be even cooler if you could store custom procedures and functions as well. This is where Object Orientation comes in. It's a very common way of writing code with many of the world's most popular languages (Java, C++, C#, Python) making use of it. You'll learn a lot more about it in A2 but there is nothing to stop you getting started now.