locked
Return New#Second Post - Rephrasing question #Unable to delete previous post RRS feed

  • Question

  • Module Module1
    
        Public Class car
            Public Property make As String
        End Class
    
        Sub main()
            returnClass().make = "Ford"
            Console.WriteLine("Value returned: {0}", returnClass().make)
            Console.ReadLine()
        End Sub
    
        Public Function returnClass() As car
            Return New car
        End Function
    
    End Module

    My Question:
    What does the statement "Return New Car" return from the function returnClass? Does it automatically create a new instance of the class named CAR and returns that? Or does it return a null value? OR does it return something else? 

    Thought process behind my code: Why is it written in the way you see?
    I was under the impression that the statement "Return New Car" is returning a new instance of the CAR class. The code is written to test that understanding. The idea was to capture the memory address (or reference) of whatever is being returned from the function returnClass . Then assign a value to the Make property and print that value. However nothing is getting printed as seen below. This means either my understanding that some reference is being passed from the function is wrong or the way I am capturing that reference in the main program is wrong. Whatever be it, I need your help to understand what is being returned by the statement "Return New Car".

    What is the source of the statement "Return New Car"?
    I came across an example with the statement "Return New Car" in the following MSDN documentation. SHARED (MSDN Documentation)

    The purpose of the example mentioned in that documentation is completely different but since I found the statement "Return New Car" a new way of using the RETURN statement (in the sense that it is not returning the name of an instance of CAR type) I just wanted to understand it better.

    Looking forward to any sort of help from you experts,

    Thanks,

    Sougata 


    Sougata Ghosh

    Sunday, September 22, 2019 1:08 PM

Answers

  • Hello,

    Each time you call returnClass it creates a new Car so setting Make then accessing returnClass again wipes out Make.

    Now this works as expected because Make is now has a Shared access modifier. Yes I changed name of the function as this makes sense.

    Module Module1
        Sub Main()
            MakeCar().Make = "Ford"
            Console.WriteLine($"Value returned: {MakeCar.Make}")
            Console.ReadLine()
        End Sub
        Public Function MakeCar() As Car
            Return New Car
        End Function
    End Module
    Public Class Car
        Public Shared Property Make 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

    • Marked as answer by sougata12 Tuesday, September 24, 2019 10:12 AM
    Sunday, September 22, 2019 1:30 PM
  • Return "New Car" returns everytime the reference to an empty Car object (instance) which is created on the heap.

    Therefore in your code you create 2 instances of a Car with the calls returnClass(), the second is shown with an empty field "make". 

    Both instances are given to the garbage collector when the method "Sub Main" goes out of scope. (After the last statement).

    Personally I find a class described inside a module very confusing. Probably it is old VB6 style. I will never do that. The only benefit I see is that you can write. Module1.Car, but then Namespaces in .Net do a better job.  


    Success
    Cor




    • Edited by Cor Ligthert Monday, September 23, 2019 12:19 PM
    • Marked as answer by sougata12 Tuesday, September 24, 2019 5:03 AM
    Monday, September 23, 2019 12:13 PM

All replies

  • Hello,

    Each time you call returnClass it creates a new Car so setting Make then accessing returnClass again wipes out Make.

    Now this works as expected because Make is now has a Shared access modifier. Yes I changed name of the function as this makes sense.

    Module Module1
        Sub Main()
            MakeCar().Make = "Ford"
            Console.WriteLine($"Value returned: {MakeCar.Make}")
            Console.ReadLine()
        End Sub
        Public Function MakeCar() As Car
            Return New Car
        End Function
    End Module
    Public Class Car
        Public Shared Property Make 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

    • Marked as answer by sougata12 Tuesday, September 24, 2019 10:12 AM
    Sunday, September 22, 2019 1:30 PM
  • Hello Karen,

    Thank you for the reply. However I have some observations:

    Since you made the Make property Shared, it means that you cannot access the Make property through instance or instance expression. If we try to access Make with an instance expression as we try to do in the statement MakeCar().Make = "Ford" , the compiler will actually access it through the Class Car and not execute the function MakeCar(). This is easily illustrated by the following code -- I have added just one "Hello" in the function MakeCar() and it does not get printed. The point is if the function MakeCar() is not being executed then of course the statement Return New Car is also not getting executed. In that case we cannot conclude conclusively determine what is being returned by the statement Return New Car.  Would you agree with my point of view?

    In other words, the question of what exactly is being passed by a statement Return New Car every time when it is being called still remains.

    Module Module1
        Sub Main()
            MakeCar().Make = "Ford"
            Console.WriteLine($"Value returned: {MakeCar().Make}")
            Console.ReadLine()
        End Sub
        Public Function MakeCar() As Car
            Console.WriteLine("Hello")
            Return New Car
        End Function
    End Module
    Public Class Car
        Public Shared Property Make As String
    End Class


    Sougata Ghosh




    • Edited by sougata12 Monday, September 23, 2019 11:07 AM
    Monday, September 23, 2019 10:15 AM
  • Return "New Car" returns everytime the reference to an empty Car object (instance) which is created on the heap.

    Therefore in your code you create 2 instances of a Car with the calls returnClass(), the second is shown with an empty field "make". 

    Both instances are given to the garbage collector when the method "Sub Main" goes out of scope. (After the last statement).

    Personally I find a class described inside a module very confusing. Probably it is old VB6 style. I will never do that. The only benefit I see is that you can write. Module1.Car, but then Namespaces in .Net do a better job.  


    Success
    Cor




    • Edited by Cor Ligthert Monday, September 23, 2019 12:19 PM
    • Marked as answer by sougata12 Tuesday, September 24, 2019 5:03 AM
    Monday, September 23, 2019 12:13 PM
  • Return "New Car" returns everytime the reference to an empty Car object (instance) which is created on the heap.

    Therefore in your code you create 2 instances of a Car with the calls returnClass(), the second is shown with an empty field "make". 

    Both instances are given to the garbage collector when the method "Sub Main" goes out of scope. (After the last statement).

    Personally I find a class described inside a module very confusing. Probably it is old VB6 style. I will never do that. The only benefit I see is that you can write. Module1.Car, but then Namespaces in .Net do a better job.  


    Success
    Cor




    Thanks for the answer. But is there a way to test that it sends across a new instance everytime it gets executed

    Sougata Ghosh

    Tuesday, September 24, 2019 6:15 AM
  • Forget thinking about sending.   

    You call twice a method which creates an instance on the heap and returns a reference (pointer to that instance). 

    Because of the way you use it, it is useless at the end of your Sub Main and therefore the Garbage Collector will release both. 


    Success
    Cor

    Tuesday, September 24, 2019 8:29 AM
  • Thanks. I take your advice, I was way too hooked on testing whether it actually sends across new instances. Hence courtsey calls me to mark Karen's answer also as she said the same thing as you. 

    Sougata Ghosh

    Tuesday, September 24, 2019 10:12 AM