Results 1 to 7 of 7

Thread: New operator

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    New operator

    Hello all
    I am new in forum and also in Visual studio
    I am trying to understand how New operator works so i have already searched msdn and found that it creates new instance
    but what does it means and how it works
    for example here is two code

    Code:
    Public Class Form1
        Private Name1 As New ArrayList
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim name As String = InputBox("Name")
            Name1.Add(name)
            ListBox1.Items.Add(name)
        End Sub
    End Class
    this code works but without New it does not
    explain me why
    Thanks in advance
    Last edited by Haake; Feb 9th, 2018 at 08:50 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: New operator

    For future reference, please use formatting tags when posting code snippets, for readability. There are buttons on the toolbar for them.
    vb.net Code:
    1. Public Class Form1
    2.     Private Name1 As New ArrayList
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         Dim name As String = InputBox("Name")
    5.         Name1.Add(name)
    6.         ListBox1.Items.Add(name)
    7.     End Sub
    8. End Class

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: New operator

    The analogy people seem to like best involves boxes.

    Let's say this code didn't use New when it created the Name1 variable.
    Code:
    Private Name1 As ArrayList
    This line only makes the variable, it doesn't store a value in the variable. It's like it built a box that's the perfect size for an ArrayList, but the box is empty. If you change the code and take out the New keyword, you'll find that this line throws a NullReferenceException:
    Code:
    Name1.Add(name)
    This is because you tried to call Add() on the ArrayList in the Name1 box, but the box is empty.

    So now we consider the line as it should be:
    Code:
    Private Name1 As New ArrayList()
    This line makes the box, but also puts an ArrayList in it. Now that we have a box, AND the box has an ArrayList in it, we're in business.

    So why don't the other lines need New? Because they don't create anything.

    Code:
    Dim name As String = InputBox("Name")
    The left side of this is "Dim name As String". That says, "Make a box that can hold a String".

    In the middle is "=". That says, "Put the thing on the right in the box on the left."

    The right side is "InputBox("Name")". InputBox() is an ancient function we don't use anymore, but here's what it does. It creates a special Form and displays it, then waits for the user to push a button. When the user pushes a button, it gets a String from a TextBox on that Form and returns it.

    So, the line all together says:
    Ask the user to enter a name, and put the String they enter into a box named 'name'.
    Next:
    Code:
    Name1.Add(name)
    'Name1' is the ArrayList we already created. 'name' is the String that InputBox() made. So this line says:
    Put 'name' into the ArrayList that is in the box named 'Name1'.
    We don't need New on this line because we're only working with things that have already been created.

    Same thing with the next line: ListBox1 was created somewhere else in the program. It has an Items property that it created itself. So we don't need to call new on anything to add 'name' to it.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: New operator

    You've only posted one code snippet there, so it's hard to compare it with another.

    Regardless, the New keyword is indeed used to create a new instance of a type. When you write code, as above, you are not writing a form object; you are writing a form type. That code is basically a template that can be followed to create objects of that type. Consider when a car company designs a car. They have a set of specifications that describe that type of car but that design is not a car. They still have to use a factory to build cars to that specification and they can build as many as they like. The ArrayList class is similarly a template and you can create as many objects of that type as you like. The New keyword is what you use to create those objects. You can create one object of that type, ten objects or a million objects but each time you want to create an object, you use the New keyword to do so. If you simply declare a variable of type ArrayList, e.g.
    vb.net Code:
    1. Private Name1 As ArrayList
    then you have not created an ArrayList object. You have set aside space in memory for an ArrayList object but there is no object there. This code:
    vb.net Code:
    1. Private Name1 As New ArrayList
    is shorthand for this:
    vb.net Code:
    1. Private Name1 As ArrayList = New ArrayList
    As you can see, the variable is declared, a new object is created and then that object is assigned to that variable. A variable refers to Nothing, i.e. no object, by default. After that code, it now refers to the new ArrayList object that was created using the New keyword.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Re: New operator

    So when I declare variable Name1 As New Arraylist I assigned it Arraylist value in which I can store other variables
    it is like declaring variables and assigning it values for example
    Code:
     
    Dim i As integer ' now it does not have value, it is empty
    i = 5 ' now it has value
    but in Name1 case it gets Arraylist value am i right?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: New operator

    Your example brings up a more complex issue.

    An integer is a value type. In your example, the variable i really IS the integer. When you have the line:

    Dim i As Integer

    the compiler sets aside enough space to hold an integer, and calls this space i. That name is really just for your benefit. The value in this memory is 0. All value types start out with an initial value, because the bytes have to hold something. It would actually be a bit dangerous if the bytes held whatever they held the last time they were used for anything, so the bytes for that integer are cleared to 0, and the integer starts with the value 0.

    When you talk about a class, though, that is a reference type. Largely speaking, you can think about it like this: Your program has the use of some large amount of the RAM on your computer. Every object, and even the code itself, takes up space in that RAM. When you pass variables to different functions, or between classes, or anything like that, you have to pass information around, which means moving it from one place to another. This can be costly. An integer takes up only four bytes, which is a compact amount, but how much does a form take up? How about a datatset that contains three datatables, each with tens of thousands of records? A form could take up hundreds, thousands, or even tens of thousands of bytes. That dataset, could take up millions of bytes quite easily. If you had to shift that around every time you passed the dataset to a method, that would be slow. Furthermore, you'd never know (though the compiler would) whether passing a certain variable around meant passing around four bytes, or four million bytes.

    So, for a reference type, the variable doesn't hold the actual object. When you write this:

    Dim nf As New Form1

    what the nf holds is not the form, but a reference to the form. Effectively, nf holds nothing more than the address of the form in memory. The form itself is somewhere out in RAM, and nf holds the location of the form. So, New is setting up that place in RAM the size of a Form1, and creating a Form1 to occupy that memory, but nf holds just the address of the memory.

    The great advantage of this is that, when you pass nf around, you aren't moving the form at all. All your are doing is copying the address around. Therefore, if I wrote:

    Dim ds As New Dataset

    then started putting millions of records into the dataset, the dataset would get larger, but the variable "ds" would stay exactly the same, small, size. So, it wouldn't matter how big the dataset got, passing ds around would always just mean passing around the address of the actual dataset.

    So, your example was a bit unfortunate because you used an integer variable as an example. Since an integer is a value type, the variable actually IS the value. But had you used a form variable as an example, the variable wouldn't be the form itself, but just the location of the form in memory.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: New operator

    Firstly, let's get the terms "declare", "create" and "assign" straight in this context. In this code:
    Code:
    Dim var As SomeType = New SomeType
    the red part is the variable declaration, the green part is object creation and the blue part is the assignment. The declaration of the variable simply tells the system to set aside some memory for a value. That memory contains all zeroes initially. In VB terms, that all-zero value represents Nothing. The New keyword is used to create a new object of the same type as the variable and the assignment operator tells the variable to refer to that new object.

    As Shaggy says, the situation is complicated somewhat by the fact that there are value types and reference type. For a value type, the variable actually contains value, rather than referring to an object that is stored elsewhere. In the case of reference types (basically any class) a variable that contains Nothing refers to no object. In the case of value types (basically and structure) a variable that contains Nothing contains the default value for that type. For numeric types, e.g. Integer, the default value is zero. For the Boolean type it is False. For the DateTime type it is #1/01/0001#. So, your example goes like this:
    vb.net Code:
    1. Dim i As Integer 'The variable contains Nothing, which is equivalent to 0.
    2.  
    3. i = 5 'The variable now contains 5.
    Going back to the ArrayList type, which is a class and thus a reference type:
    vb.net Code:
    1. Dim obj As ArrayList 'The variable contains Nothing, which means it refers to no object.
    2.  
    3. obj = New ArrayList 'The variable now refers to the newly created ArrayList object.
    4.  
    5. obj.Add(5) 'The ArrayList object that the variable refers to now contain an item with value 5.
    If we were to omit the line with the New keyword:
    vb.net Code:
    1. Dim obj As ArrayList 'The variable contains Nothing, which means it refers to no object.
    2.  
    3. obj.Add(5) 'This will throw a NullReferenceException because you cannot call a method of an object that doesn't exist.
    This:
    vb.net Code:
    1. Dim obj As ArrayList
    2.  
    3. obj = New ArrayList
    is functionally equiavlent to this:
    vb.net Code:
    1. Dim obj As ArrayList = New ArrayList
    and this:
    vb.net Code:
    1. Dim obj As New ArrayList

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width