logo VB.Net - Write Binary File
Guide Contents
DigitalDan Home Page

Structures
Some VB.Net textbooks manage to dedicate complete chapters to structures without explaining what they are.
In it's most common form, a Structure allows two or more variables to be merged together into a single object.
When writing a functionm that returns a fraction, you face a problem - the fraction has two parts (top / bottom) but functions can only return one object.
Using a structure, we can merge top and bottom into one object. The function can return the object and subsequent code can extract top and bottom from this object.
 
Structures are normally defined after the end of the form code i.e. after the End Class statement. The format is as follows
 
Public Structure Fraction
Dim top As Integer
Dim bottom As Integer
End Structure

 
Our structure must be Public (or Friend) becaise it is located outside of the main program code. The variables top and bottom will hold the two parts of our fraction.
 
When setting the variables top and bottom, we need to tell the compiler that they are located inside the structure called Fraction. This is achieved by prefixing our varables with a name referencing the structure and a dot. Apart from that, we access the structure parts in the same way as any other variable and this is done within the normal program code. Here is an example
 
Private Shared Function GetFraction(percent As Integer) As Fraction
Dim frac1 As New Fraction
Dim i As Integer = 2
frac1.top = percent
frac1.bottom = 100
While i <= frac1.bottom
If (frac1.top Mod i = 0) And (frac1.bottom Mod i = 0) Then
frac1.top = frac1.top \ i ' the \ means integer division
frac1.bottom = frac1.bottom \ i ' the \ means integer division
Else
i += 1
End If
End While
Return frac1
End Function

 
In order to read the varaible within the structure, we again prefic the variable name with the structure reference and a dot. Here is an example
 
Private Function GetPercent(frac2 As Fraction) As Double
Return 100 * frac2.top / frac2.bottom
End Function

 
The main things to rememeber are - Structures are normally defined after the end of our program code. The compiler is unlikely to find "Private" structures located away form the main code and you will have to make it Friend or Public. You can use as many structures as you like. Structures can include any object or variable types including byte, integer, double, string, list(of object), arrays or even other structures.
 
There are many uses for structures, apart from functions, especially when creating arrays/lists of multi-part data elements e.g.
 
Dim ListFractions As New List(Of Fraction)
Dim frac3 As Fraction
ListFractions.Clear()
frac3.top = 1 : frac3.bottom = 8 : ListFractions.Add(frac3)
frac3.top = 1 : frac3.bottom = 4 : ListFractions.Add(frac3)
frac3.top = 3 : frac3.bottom = 8 : ListFractions.Add(frac3)
frac3.top = 1 : frac3.bottom = 2 : ListFractions.Add(frac3)
frac3.top = 5 : frac3.bottom = 8 : ListFractions.Add(frac3)
frac3.top = 3 : frac3.bottom = 4 : ListFractions.Add(frac3)
frac3.top = 7 : frac3.bottom = 8 : ListFractions.Add(frac3)

DigitalDan.co.uk ... Hits = 354