The new concept (part 1)
The new function
In earlier articles, I have introduced a very common error The error message that I dealt with was: Object reference not set to instance of year year object. I used the functions Try and Catch as a workaround.
Unfortunately for my last example, I used an array of string to cause the error. The problem with the use of the absence of string is the construction of an object.
To better understand the concept of object creation, take the example with the use of a class. Consider the following code example:
I build 2 files: one for the form and one for a class.
''' <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 Try Catch ex As Exception 'add basic error message here MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message) End Try End Sub End Class |
Public Class Class1 Public File As String Public Path As String Public Extension As String End Class |
We could put everything in the same file, but I do not think it is a good practice to use. Yet it is to remember that the goal in this article is to highlight the use of the New function.
Now, in your code at the beginning of your function button, declare your variable as follows (I'll explain later):
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim oClass1 As Class1 'Initiate variable Try Catch ex As Exception 'add basic error message here MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message) End Try End Sub |
Inside your Button function, after the Try , add the line :
oClass1 = New Class1
Your code should look like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim oClass1 As Class1 'Initiate variable Try oClass1 = New Class1 'create object Catch ex As Exception 'add basic error message here MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message) End Try End Sub |
Explanations:
OK, when you declare a variable as a string (String), an integer (Integer), Boolean (Boolean), a bit, a byte, a real number .... It tells the computer you will need to use a string, an integer, a boolean value ... and the computer creates and reserves memory space accordingly. Some of these things take more memory than others. This is true and only true for the basic stuff. I mean stuff such as String, Integer, Boolean, etc..
However, the Class1 file is not a basic thing, it is an object. Although simple in appearance, it consists of several new topics of articles I prefer to reserve for another occasion.
What to remember here is that Class1 is actually a class. Why? simply because it is written that it is a class in the first line and the last line to identify the beginning and the end.
Within the first line and the last line, there are 3 lines that look are a statement of 3 strings (String).
So what is a class? Is just something bigger, with more stuff and more elements. That is all.
And each element requires complicated computer calculation space to use and are much more than basic things such as String, Integer, Boolean ... It is for this reason that each object declaration and not simple variables, you must provide a line to create this object. However, the line just after the line Try complete the creation of the object of type Class1.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim oClass1 As Class1 'Initiate variable Try oClass1 = New Class1 'create object Catch ex As Exception 'add basic error message here MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message) End Try End Sub |
I must admit that the idea of ??creating a class or object is difficult to understand. It is somewhat abstract and not concrete. Personally, I have long prayed what a class without really having understood as today.
But we can always explain and give illustrations, but nothing beats practice that I provided in an upcoming blog post.
Buy the same edition I bought:
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.
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :