Friday, October 19, 2012

The most important thing in the new function

The most important thing in the new function

The new function - a big concept


One mistake that programmers make is the concept of object and pointer. What? Stay calm, this article is not a computer course or philosophy.
Let's say you have a fairly large file on your computer. This file is saved in your documents folder or on the hard disk, memory card, USB DVD disc, finally, you understand??
More files you have, the longer it takes up space and it's the same principle with the objects created in the computer codes.
Okay, I'm sure you understand.

Now imagine that in your computer, you have shortcuts fort files, bookmarks, favorites, and finally things that help you better pack your files in one place. Imagine that on your computer desktop you have a shortcut that leads to a music file, a folder of work, you must understand that the shortcut is not the file. Do you understand?





Computing and programming is the same. When we program, we must keep in mind that that you must know what you want to create. Okay, it's hard for me to explain more, go through example code and see the result.

The Form1.vb file

''' <summary>
''' improve your coding
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
    ''' <summary>
    ''' start button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim oClass1 As Class1 'Initiate variable
        Dim oCollection As Collection 'Initiate variable
        Try
            oClass1 = New Class1 'create object but outside the loop
            oCollection = New Collection 'create a collection
            For index1 = 1 To 5 Step 1 'simple loop from
                'If oClass1 Is Nothing Then MsgBox("oClass1 is nothing")


                oClass1.File = index1.ToString  ' fill the String named File (a value)
                oClass1.Extension = ".txt" ' fill the String named Extension
                oClass1.Path = "c:\" ' fill the String named Path

                oCollection.Add(oClass1) 'add the object (???) in a collection of objects




            Next index1

            'display informations from oClass1 to form and textbox
            TextBox1.Text = oCollection.Item(1).File 'display the file  from the 1st item in collection
            TextBox2.Text = oCollection.Item(2).File 'display the file  from the 2nd item in collection
            TextBox3.Text = oCollection.Item(3).File 'display the file  from the 3rd item in collection
            TextBox4.Text = oCollection.Item(4).File 'display the file  from the 4th item in collection
            'please notice that all item in oCollection are the same.
            'The 4 items in the collection are looking at the same and single object.

        Catch ex As Exception
            'add basic error message here
            MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message)
        End Try

       
    End Sub
End Class




The class1.vb file.

Public Class Class1
    Public File As String
    Public Path As String
    Public Extension As String

End Class




You need to understand what is happening in the code, or at least have an idea of ​​how.

The object oClass1 is created with the new function. You follow me?
Then the code enters a loop where it adds a collection of Class1 object according to different values. You see?
When it comes time to view the code, you will realize that the results are identical, despite the fact that the code is displaying four different elements of the collection of objects. Is what you see? Otherwise, run the code and observe.



Explanations:

The object is actually created oClass1 once. When the code enters the loop, it assigns the value to File object in the same oClass1. The function add collection merely creates a shortcut that points to the single object that is created oClass1
So we end up with 4 items in the collection are in fact four shortcuts pointer pointing exactly at the same place in the computer.
So what if we want to create four distinct objects? It's very simple. To the line with the new function within the for loop.
The program will run repeated object creation and then be informed by the various assignments of text, number, etc..
We would have a collection with four shortcuts. Each of these shortcuts will point to four separate objects.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim oClass1 As Class1 'Initiate variable
        Dim oCollection As Collection 'Initiate variable
        Try
            'oClass1 = New Class1 'create object but outside the loop
            oCollection = New Collection 'create a collection
            For index1 = 1 To 5 Step 1 'simple loop from
                'If oClass1 Is Nothing Then MsgBox("oClass1 is nothing")

                oClass1 = New Class1 'create object but inside the loop
                oClass1.File = index1.ToString  ' fill the String named File (a value)
                oClass1.Extension = ".txt" ' fill the String named Extension
                oClass1.Path = "c:\" ' fill the String named Path

                oCollection.Add(oClass1) 'add the object (???) in a collection of objects




            Next index1

            'display informations from oClass1 to form and textbox
            TextBox1.Text = oCollection.Item(1).File 'display the file  from the 1st item in collection
            TextBox2.Text = oCollection.Item(2).File 'display the file  from the 2nd item in collection
            TextBox3.Text = oCollection.Item(3).File 'display the file  from the 3rd item in collection
            TextBox4.Text = oCollection.Item(4).File 'display the file  from the 4th item in collection
            'please notice that all item in oCollection are the same.
            'The 4 items in the collection are looking at the same and single object.

        Catch ex As Exception
            'add basic error message here
            MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message)
        End Try

       
    End Sub


I have to make a point. The first code quoted in this article is not something to be avoided. Why? Because it highlights the use of pointers (shortcuts) over the manipulation of objects. As far I can remember, manipulate shortcuts is 1000 times faster than the object. An object, it is a finished product with a dimension. The pointer is lightweight and provides information to the computer where to look to find the object.

In older programming language, to read the C / C + +, pointers very easy to identify due to the presence of an asterisk (*). Today, the concept is more simple.



The program I love to use:



About

I invite you to visit my blog for more articles and leave a comment.
Check Technologies represents more than 10 years .... Computer and computer aided design.






No comments:

Post a Comment