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[edit | edit source]
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:
Const heart as integer = 1 Const club as integer = 2 Const spade as integer = 3 Const diamond as integer = 4 dim cardset as string cardset = spade
We can bring them together in a nice neat structure called an enum:
Enum suits HEARTS = 1 CLUBS = 2 SPADES = 3 DIAMONDS = 4 End Enum dim cardset as suits cardset = suits.HEARTS
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
Const Win as Integer = 3 Const Draw as Integer = 1 Const Lose as Integer = 0
With enums we can create a datatype called Result and store the points within it, under easy to remember name:
Enum Result Win = 3 Lose = 1 Draw = 0 End Enum dim ManUvChelsea as Result ManUvChelsea = Result.Win Console.Writeline("ManU scored " & ManUvChelsea & " points" )
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
Records and Fields[edit | edit source]
|In VB, records are known as structures|
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:
Dim studentID As Integer Dim studentName As String Dim studentDoB As Date
Sub Main() Console.write("insert the id: ") newStudentid = console.readline() console.write("insert the name: ") newStudentname = console.readline() console.write("insert the Date of Birth: ") newStudentDoB = console.readline() console.writeline("new record created: " & newStudentid & " " & newStudentname & " " & newStudentDoB) End Sub
For the following input:
But what if your college has more than one student, we'd have to write:
Dim studentID1 As Integer 'field Dim studentName1 As String 'field Dim studentDoB1 As Date 'field Dim studentID2 As Integer 'field Dim studentName2 As String 'field Dim studentDoB2 As Date 'field Dim studentID3 As Integer 'field Dim studentName3 As String 'field Dim studentDoB3 As Date 'field ... ... Dim studentID2400 As Integer 'field Dim studentName400 As String 'field Dim studentDoB400 As Date 'field
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:
Structure student 'record declaration Dim id As Integer 'field Dim name As String 'field Dim DoB As Date 'field End Structure Sub Main() Dim newStudents(400) As student 'declare an array of student records, a school with 401 students for x = 0 to 400 'insert the details for each student Console.WriteLine("insert the id") newStudents(x).id = Console.ReadLine() Console.WriteLine("insert the name") newStudents(x).name = Console.ReadLine() Console.WriteLine("insert the Date of Birth") newStudents(x).DoB = Console.ReadLine() next for x = 0 to 400 'print out each student Console.WriteLine("new record created: " & newStudents(x).id & " " & newStudents(x).name & " " & newStudents(x).DoB) next End Sub
This seems to solve our problem, you might want to try it out yourself but decrease the number of students slightly!
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
'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.
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.