Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

VB.NET from beginner to advanced programmer Part 17 - Structures

VB.NET form myvariable

  • Please log in to reply
2 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 01 November 2009 - 05:23 PM

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





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:

Structure Users

End Structure

However, now the structure is empty so we'll receive this error:

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:


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:

        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

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.

If I now take the above code and put it into a form.load event the whole thing should look like this:

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

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.






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:

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

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:

    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:

    Private myVariable As Integer
    ReadOnly Property myProperty() As Integer
        Get
            Return myVariable
        End Get
    End Property

or only allow writing by doing a writeonly property:

    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:

    Event valueChanged(ByVal fromValue As Integer, ByVal toValue As Integer)

In the above example I created an Event(valueChanged) with two Integer parameters(fromValue,toValue).


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:


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 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:

    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

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:

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.

Edited by Vswe, 21 March 2010 - 02:37 PM.

  • 1

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 02 November 2009 - 05:59 AM

Nicely done! Is this why you were asking the difference between classes and structures?
+rep
  • 0

#3 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 02 November 2009 - 06:01 AM

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.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download