none
Derived Class RRS feed

  • Question

  • I have a class Vehicle with properties Reg, Make, Model with instance vehicle and a class NewVehicle with instance newVehicle extends Vehicle.  Both are populated from reads on different databases.  I am comparing vehicle.GetReg() and newVehicle.GetReg() however, when populating newVehicle, as I step through the code, after newVehicle.setReg() executes, I noticed that vehicle.getReg() changes to newVehice.SetReg().  No idea why this is happening.
    Sunday, December 3, 2017 2:37 AM

All replies

  • Hello,

     Very good description but we need to see the code you have that causes the issue.

    Yes, we all could build a project based on your description. However, we most likely

    will not get the same results that caused your concern.

     Thanks :)

    Sunday, December 3, 2017 2:49 AM
  • Hello Richard,

    >>after newVehicle.setReg() executes, I noticed that vehicle.getReg() changes to newVehice.SetReg().  No idea why this is happening

    I can't assume what has happened in your case. When you create vehicle instance and NewVehicle instance. Them have different heap memory to storage fields. But for the newvehicle method, if the newvehicle type doesn't rewrite the base class method. Their method both point to the same place. So when you execute setReg method, it will step through the base class method.

    It would be nice if you provide some relating code.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 5, 2017 9:53 AM
    Moderator
  • Hi RichardDunneBSc,

    Based on your description, I am not sure what cause the issue, could you please provide a bit more information, such as related detailed code, related data table, and describing the issue in detailed. it will be beneficial to resolve the issue.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 13, 2017 6:41 AM
    Moderator
  • Yes that is moslty caused by a old misunderstanding. 

    Working with references does not mean you create a new object if you copy another. You only create a new reference to the same object. 

    There are few methods in .Net which create a deepcopy

    Keep in mind that despite it for you maybe suggest that methods with the name likewise Copy or Clone creates copy the never do. 

    Methods with the name CopyTo do that, but they are seldom in classes in .Net. 


    What you describe is typical for wrong understanding the copying of reference types. There is simply pointed to the same object.


    Success
    Cor


    • Edited by Cor Ligthert Wednesday, December 13, 2017 12:41 PM
    Wednesday, December 13, 2017 12:41 PM