Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
Much information awaits you now in the series' longest part, it is about structures. A structure is a container type, which means it contains other types as members. For example: variables, properties, functions etc. When using Structures you first write the code to the base and then you can create new instances of the base so you have all the members of the structure once for each instance of the structure you have. An example of when you could use them is when you have some clients, you're creating a structure called client and then for each client you're creating an instance for it. In the structure you could have a variable called name, one called company etc. Then each instance of the structure could have different values on the variables since all clients do probably not have the same name. You'll soon understand how it works.
To create a new structure (which I'll name Users) we're simply doing it like this:
However, now the structure is empty so we'll receive this error:Code:Structure Users End Structure
Code:Structure 'Users' must contain at least one instance member variable or Event declaration.
So then we adds a variable or two. Now we need to remember a thing about scopes, If we declare this variable as private we'll only be able to access it from within the structure itself, so therefor I will declare it as public:
Code:Structure Users Public Name As String Public Money As Integer End Structure
So now the Structure Users have one Public String named Name and one Integer named Money. Even though this structure is very simple I can show you how we may use a structure after we've created it. Observe that this code below is not inside the Structure, I will later show you all the code(the structure and when we're using it) together so you'll see where everything is:
So in this small example above I first declared the Variable User1 as the type Users which is our structure, then I set the value of the two variables Name and money. Then I'm doing the same with a variable called User2. At the end, just to show you, I compare the amount of money the two users have.Code:Dim User1 As Users User1.Name = "Me" User1.Money = 5 Dim User2 As Users User2.Name = "You" User2.Money = 5 If User1.Money = User2.Money Then MessageBox.Show("You have the excactly same amount of cash") End If
If I now take the above code and put it into a form.load event the whole thing should look like this:
Other members(for example constants and function) are working pretty much the same as the variable. However, there's two member that is new, properties and events.Code:Public Class frmMain Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim User1 As Users User1.Name = "Me" User1.Money = 5 Dim User2 As Users User2.Name = "You" User2.Money = 5 If User1.Money = User2.Money Then MessageBox.Show("You have the excactly same amount of cash") End If End Sub End Class Structure Users Public Name As String Public Money As Integer End Structure
Properties
Properties are used to Get or Set variables in the Structure but wants to do more the just Set/Get it, for example you maybe wants to test if a value is valid before you adds it and when you do you maybe wants to modify the value a bit before you adds it.
This is an example one a property:
In the structure in the example above we have a private variable which the property modify and reads from. When we're using the structure it works exactly the same as with variables, in fact in the example above we could just have had a public variable called myProperty in the structure to get the same result. But as I said before we can use them to do other things before actually setting/getting the value, for example a property could look like this:Code:Public Class frmMain Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myInstance As myStructure myInstance.myProperty = 10 MessageBox.Show(myInstance.myProperty) End Sub End Class Structure myStructure Private myVariable As Integer Property myProperty() As Integer Get Return myVariable End Get Set(ByVal value As Integer) myVariable = value End Set End Property End Structure
Code:Private myVariable As Integer Property myProperty() As Integer Get If myVariable > 20 Then Return myVariable Else Return 0 End If End Get Set(ByVal value As Integer) If value <> 0 Then myVariable = 100 / value End If End Set End Property
If you only want to allow reading, you can do a readonly property:
or only allow writing by doing a writeonly property:Code:Private myVariable As Integer ReadOnly Property myProperty() As Integer Get Return myVariable End Get End Property
Code:Private myVariable As Integer WriteOnly Property myProperty() As Integer Set(ByVal value As Integer) myVariable = value End Set End Property
Events
We've used events before and learned us about them early, but not how to crate your own:
In the above example I created an Event(valueChanged) with two Integer parameters(fromValue,toValue).Code:Event valueChanged(ByVal fromValue As Integer, ByVal toValue As Integer)
To have any use of the event we need to raise it. When the event is raised we can catch that up by some code, for example the form.load event is raised when the form is loaded and then we can use that to run some code when the form loaded. I've used the above event in a structure together with a writeonly property:
So each time we're changing the value of the property called myProperty the valueChanged event is raised with the old value and the new value. To be able to do something each time this event is raised for our instance of the structure we have to do something like this:Code:Structure myStructure Private myvariable As Integer Public WriteOnly Property myProperty() As Integer Set(ByVal value As Integer) RaiseEvent valueChanged(myvariable, value) myvariable = value End Set End Property Event valueChanged(ByVal fromValue As Integer, ByVal toValue As Integer) End Structure
So above I created a new instance of the structure called myStructure, then I added a handler for it. To do it you first write "AddHandler " then the name of the instance dot the event ("myInstance.valueChanged"). This is to select what event we will add, then we have to choose where by ending with ", AddressOf mySub" which adds it to the sub called mySub. Observe that I've given mySub two Integer parameters just as the event but note that they doesn't have to be named the same. So when we change the property myProperty it will raise the event and since we added a handler to mySub, the code in mySub will be run. See this example:Code:Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myInstance As myStructure AddHandler myInstance.valueChanged, AddressOf mySub End Sub Private Sub mySub(ByVal oldValue As Integer, ByVal newValue As Integer) End Sub
Code:Public Class frmMain Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myInstance As myStructure AddHandler myInstance.valueChanged, AddressOf mySub myInstance.myProperty = 20 myInstance.myProperty = 20 myInstance.myProperty = 10 End Sub Private Sub mySub(ByVal oldValue As Integer, ByVal newValue As Integer) If oldValue <> newValue Then MessageBox.Show("The value changed from " & oldValue & " to " & newValue) End If End Sub End Class Structure myStructure Private myvariable As Integer Public WriteOnly Property myProperty() As Integer Set(ByVal value As Integer) RaiseEvent valueChanged(myvariable, value) myvariable = value End Set End Property Event valueChanged(ByVal fromValue As Integer, ByVal toValue As Integer) End Structure
So we have the structure with the property and the event, we adds the handler of that event to mySub, which will check if the two values are the same or not. If they isn't it prints out how the value changed.
After adding the handler we changed myProperty to 20. This will get us the output "The value changed from 0 to 20". Then we set the property to 20 again but this time nothing will happen since it's the same value. Lastly we set myProperty to 10 which of course gives us this output: "The value changed from 20 to 10".
And now we have come to the end of this part, see you later in next part.
Last edited by Vswe; 03-21-2010 at 03:37 PM.
Nicely done! Is this why you were asking the difference between classes and structures?
+rep
Actually, that is the next part. I wanted a short definition of the difference, now I just explained the most important difference without an actually definition.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks