none
Role of the NEW keyword RRS feed

  • Question

  • Hi,

    In the below code (Example 1) the following error comes in the line "instanceVar = returnClass()". However when I use the NEW keyword,i.e. if I write Dim instanceVar As New shareTotal() the error goes away. Can someone explain why is this happening?

    The error is as follows:

    'Example 1

    Module Module1 Public Class shareTotal 'Nothing End Class Sub main() Dim instanceVar As shareTotal() 'When NEW is used the error goes away Dim check As Boolean instanceVar = returnClass() 'error comes here check = instanceVar Is returnClass() Console.WriteLine("Result of (instanceVar Is returnClass) check: {0}", check) Console.ReadLine() End Sub Public Function returnClass() As shareTotal Return New shareTotal End Function End Module


    The reason I am asking this question is because in the below example (Example 2), I can assign instance1 to instance2 although while creating instance2 we did not use the NEW keyword...this is exactly what I am trying to do in the example1 (i.e.assigning the instance returned by the function returnClass to instanceVar...but then where am I going wrong?

    'Example 2

    Module Module1 Public Class shareTotal 'Nothing End Class Sub main() Dim instance1 As New shareTotal() Dim instance2 As shareTotal instance2 = instance1 'Works fine End Sub End Module


    Sougata Ghosh


    • Edited by sougata12 Tuesday, November 12, 2019 10:21 AM
    Tuesday, November 12, 2019 10:20 AM

Answers

  • The way I understand NEW is that it creates a new space in the memory for the corresponding object/instance. So when in example 2 in my question, the statement, Dim instance1 as New shareTotal(), the compiler shall create a space in memory and store the address of that location in instance1. When the statement instance2 =instance1 gets executed then the  same address is getting stored in the variable instance2..with no new memory getting allocated. Like a second handle getting attached to an existing bucket.

    The compiler is not creating an instance of the object with memory reserved for the object. It's the .NET runtime that is doing it.

    And instance2 object is being replaced by instance1. 

    If shareTotal had public properties and you populated data into the properties of instance1,

    then the data in instance1 would be in instance2 as instance2 became instance1.

    Using the above logic, I was of the opinion that in example 1 of my question, when the Return New shareTotalstatement gets executed, it is allocating a new memory space for a new instance.    

    Correct the sharTotal object is instanced into its own memory allocated for it  

     I was expecting instanceVar in example 1 to behave similarly as instance2 in example 2 but clearly that is not the case. The moment I use NEW to instantiate instanceVar, it works…..but then does it not mean we are allocating two different memory spaces (once with Return New shareTotal and the other time with Dim instanceVar as New shareTotal()?

    PLease help me understand where I am going wrong?

    You could just do this in example1

    Dim instanceVar = returnClass()  

    You would not need to do the below line

     Dim instanceVar As shareTotal() 'When NEW is used the error goe

    The statment below would strong type instanceVar as a sharTotal object in the local scope of the method as the function is executed to return the object

    Dim instanceVar = returnClass()  

    What lead you to using Module in this situation, which seems kind of strange?


    • Edited by DA924x Wednesday, November 13, 2019 8:50 AM
    • Marked as answer by sougata12 Wednesday, November 13, 2019 6:43 PM
    Wednesday, November 13, 2019 8:48 AM

All replies

  • A code module is considered a static class (in C#) which in both languages does not need to created with the New keyword meaning you can use a code module over and over while a class you can create one or more instances and none of them see the other instance.

    See also: difference between classes and modules.

    In the following example I created two instance of Person and they both stand alone while in Whatever module any changes are of one instance.

    Module Module1
    
        Sub Main()
            Dim person1 As New Person With {.FirstName = "Karen", .LastName = "Payne"}
            Dim person2 As New Person With {.FirstName = "Joe", .LastName = "Smith"}
    
        End Sub
    
    End Module
    Module Whatever
        Public FirstName As String = "Alex"
        Public LastName As String = "Jones"
    End Module
    Public Class Person
        Public Property FirstName() As String
        Public Property LastName() As String
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, November 12, 2019 10:38 AM
    Moderator
  • The 'New' keyword instances a class into an object and the object lives in memory.

    It doesn't matter if you are using VB or C# on the .NET lanuage platform or Java they are all object oriented languages. 

    They all  follow the OO princeples of class vs object vs instance. 

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    But take this into consideration too concerning the 'new is glue' principle.

    https://ardalis.com/new-is-glue



    • Edited by DA924x Wednesday, November 13, 2019 12:33 AM
    Wednesday, November 13, 2019 12:32 AM
  • Hi DA924x/Karen,

    The way I understand NEW is that it creates a new space in the memory for the corresponding object/instance. So when in example 2 in my question, the statement, Dim instance1 as New shareTotal(), the compiler shall create a space in memory and store the address of that location in instance1. When the statement instance2 =instance1 gets executed then the  same address is getting stored in the variable instance2..with no new memory getting allocated. Like a second handle getting attached to an existing bucket.

     

    Using the above logic, I was of the opinion that in example 1 of my question, when the Return New shareTotal statement gets executed, it is allocating a new memory space for a new instance. The address of that new memory space should get stored in instanceVar when instanceVar = returnClass() gets executed. In other words, I was expecting instanceVar in example 1 to behave similarly as instance2 in example 2 but clearly that is not the case. The moment I use NEW to instantiate instanceVar, it works…..but then does it not mean we are allocating two different memory spaces (once with Return New shareTotal and the other time with Dim instanceVar as New shareTotal()?

    PLease help me understand where I am going wrong?



    Sougata Ghosh



    • Edited by sougata12 Wednesday, November 13, 2019 3:09 AM
    Wednesday, November 13, 2019 3:05 AM
  • Hi DA924x/Karen,

    The way I understand NEW is that it creates a new space in the memory for the corresponding object/instance. So when in example 2 in my question, the statement, Dim instance1 as New shareTotal(), the compiler shall create a space in memory and store the address of that location in instance1. When the statement instance2 =instance1 gets executed then the  same address is getting stored in the variable instance2..with no new memory getting allocated.

     

    Using the above logic, I was of the opinion that in example 1 of my question, when the Return New shareTotal statement gets executed, it is allocating a new memory space for a new instance. The address of that new memory space should get stored in instanceVar. I was expecting instanceVar in example 1 to behave similarly as instance2 in example 2 but clearly that is not the case. The moment I use NEW to instantiate instanceVar, it works…..but then does it not mean we are allocating two different memory spaces (once with Return New shareTotal and the other time with Dim instanceVar as New shareTotal()?

    PLease help me understand where I am going wrong?



    Sougata Ghosh

    Did you read the link in my post?

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/#differences-between-classes-and-modules


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 13, 2019 3:07 AM
    Moderator
  • Yes Karen..I did....I feel there are two key points mentioned in the section"
    1. object data exists separately for each instantiated object
    2. 
    Members defined within a class are scoped within a specific instance of the class and exist only for the lifetime of the object. To access class members from outside a class, you must use fully qualified names in the format of Object.Member

    I understand both these statements in a stand-alone manner but I am unable to connect these two ideas to resolve my doubt. Would it be possible that you explain the connection in simpler words?


    Sougata Ghosh


    • Edited by sougata12 Wednesday, November 13, 2019 5:13 AM
    Wednesday, November 13, 2019 5:05 AM
  • The way I understand NEW is that it creates a new space in the memory for the corresponding object/instance. So when in example 2 in my question, the statement, Dim instance1 as New shareTotal(), the compiler shall create a space in memory and store the address of that location in instance1. When the statement instance2 =instance1 gets executed then the  same address is getting stored in the variable instance2..with no new memory getting allocated. Like a second handle getting attached to an existing bucket.

    The compiler is not creating an instance of the object with memory reserved for the object. It's the .NET runtime that is doing it.

    And instance2 object is being replaced by instance1. 

    If shareTotal had public properties and you populated data into the properties of instance1,

    then the data in instance1 would be in instance2 as instance2 became instance1.

    Using the above logic, I was of the opinion that in example 1 of my question, when the Return New shareTotalstatement gets executed, it is allocating a new memory space for a new instance.    

    Correct the sharTotal object is instanced into its own memory allocated for it  

     I was expecting instanceVar in example 1 to behave similarly as instance2 in example 2 but clearly that is not the case. The moment I use NEW to instantiate instanceVar, it works…..but then does it not mean we are allocating two different memory spaces (once with Return New shareTotal and the other time with Dim instanceVar as New shareTotal()?

    PLease help me understand where I am going wrong?

    You could just do this in example1

    Dim instanceVar = returnClass()  

    You would not need to do the below line

     Dim instanceVar As shareTotal() 'When NEW is used the error goe

    The statment below would strong type instanceVar as a sharTotal object in the local scope of the method as the function is executed to return the object

    Dim instanceVar = returnClass()  

    What lead you to using Module in this situation, which seems kind of strange?


    • Edited by DA924x Wednesday, November 13, 2019 8:50 AM
    • Marked as answer by sougata12 Wednesday, November 13, 2019 6:43 PM
    Wednesday, November 13, 2019 8:48 AM
  • Hi,

    The compiler is not creating an instance of the object with memory reserved for the object. It's the .NET runtime that is doing it.Thanks for pointing this out..

    What lead you to using Module in this situation, which seems kind of strange?If you are referring to the fact that I have placed the CLASS inside the module then yes it is due to my bad programming habits....shall change it

    Coming to the main question that I asked:

    The answer that you gave works....so thanks for it. However I want to share with the community another solution which I feel is simpler. In example 1, the first statement in the Sub Main() is: Dim instanceVar As shareTotal()

    I think the parenthesis after shareTotal is what is creating a problem...its like I am not using the NEW keyword but trying to call a constructor. Once I remove the brackets its works fine. My understanding behind this is as follows...please let me know if I am thinking correctly:

    1. The statement Dim instanceVar As shareTotal creates a variable of shareTotal type, the keyword being 'variable' as the intent is not to instantiate.

    2. The function returns a newly created object

    3. This newly created object gets assigned to the variable instanceVar

    @DAx...I am leaving this thread open for the time being to ensure that you receive the notification of this message.

    Although this message is in reply to Dax, this being an open forum any other person's view is most welcome.

    • Edited by sougata12 Wednesday, November 13, 2019 11:53 AM
    Wednesday, November 13, 2019 11:38 AM
  • I think the parenthesis after shareTotalis what is creating a problem...its like I am not using the NEW keyword but trying to call a constructor. Once I remove the brackets its works fine. My understanding behind this is as follows...please let me know if I am thinking correctly:

    Yes, I was going to make mention of the Dim instanceVar As shareTotal  is the proper way of defining a placeholder variable sort of speaking for an object instance, a type, that will eventually hold a shareTotal object instance, the same type,  with object instance being created elsewhere in the code.

    2. The function returns a newly created object

    Correct...

    3. This newly created object gets assigned to the variable instanceVar

    Correct...

    Just FYI..

    If shareTotal class was defined in namespace sougata12 resulting in sougata12.shareTotal and shareTotal class was defined in DA924x namespace resulting in DA924x.shareTotal, then .NET runtime will not allow sougata12.shareTotal = DA924x.shareTotal, becuase the classes/objects are defined in two different namespaces and a cast exception will be thrown by .NET runtime.

    Wednesday, November 13, 2019 5:54 PM
  • If shareTotal class was defined in namespace sougata12 resulting in sougata12.shareTotal and shareTotal class was defined in DA924x namespace resulting in DA924x.shareTotal, then .NET runtime will not allow sougata12.shareTotal = DA924x.shareTotal, becuase the classes/objects are defined in two different namespaces and a cast exception will be thrown by .NET runtime.

    Thanks for the above piece of info….


    Sougata Ghosh

    Wednesday, November 13, 2019 6:45 PM