Overwriting a class reference

nismo

Member
Joined
Jun 30, 2006
Messages
18
Programming Experience
3-5
I have a problem.



I have an instance of a class that has been passed to my form. What happens is, i create a copy of the class and the changes the user makes is applied to the copy. If the user cancels, i just abandon the copied class, but if they want to save, then i want to overwrite the class that was passed to my form with the copied version. So that the class instance on the calling screen is updated with the new (copied) class.



How can i do it?
 
If you want to change the property values of an object then you do exactly that: you change the property values of the object. You've already set the property values if the copy. Just set the same properties of the original to the same values.
 
Thanks for the reply. I dont think you understand what im trying to do though...

I have form A. When a user presses "Select Flight" on form A, it loads form B (which is the flight select screen). It passes an instance of the Search class which contains a number of different peices of information.


So for example, this is what form B (select flight) looks like;
Public Sub New(ByRef SearchInst As ScheduledFlightSearch)
MyBase.New()
InitializeComponent()

SearchCopy = CopyObject(SearchInst)
ActualSearch = SearchInst
End Sub

I pass a search instance to that form. That form creates a copy (as you can see). The form then works against the copy, which means the original search instance on form A remains untouched. This provides an easy cancel facility if the user choses not to save the changes.

My problem is how to save the changes. How would you save the changes? You dont have direct access to form A, so you cant just got FormA.Search = SearchCopy.

Setting SearchInst = SearchCopy wont work either because it doesnt update the reference on the calling form.

I need to make it so that the memory location for SearchInst is changed to the memory location for SearchCopy.

I can not manually populate the class, there is way to much information stored in the class to make it a viable option...
 
I can not manually populate the class, there is way to much information stored in the class to make it a viable option...
If that's what you really think then I suggest you give up programming right now.

You've got a reference to the original object in your ActualSearch field. Set its property values. If you need to lie down afterwards then so be it. ;)
 
When you're passing the reference type instance ByRef that parameter is the same reference as the original, if it was ByVal a copy of the reference would be created referring to the same instance. So with your ByRef all you have to do is assign it a new object and the caller will see the same object.
Sample, call the Caller:
VB.NET:
Sub caller()
    Dim x As New test
    x.x = "old"
    reff(x) 'after this x refers to a different object than was initially created
    Dim s As String = x.x
    Stop
End Sub

Sub reff(ByRef y As test)
    y = New test
    y.x = "new"
End Sub

Class test
    Public x As String
End Class
 
If that's what you really think then I suggest you give up programming right now.

You've got a reference to the original object in your ActualSearch field. Set its property values. If you need to lie down afterwards then so be it. ;)

But some of the class members are instances of other classes... and i dont want to have to maintain hoardes of code that just duplicates a class (because then i have to remember to go back and change it if i add members to it)
 
When you're passing...

Thats not what im trying to do... I know i can easily update each member of a class that is passed to a function, but i want to overwrite the source of that passed instance with a new instance, so that in the calling function (caller) the instance of x is replaced by the new instance created in the function 'reff' (y).

In your example, you pass an instance of 'test' to the reff sub. How would you make the souce of y (x) equal to the new class that you created in the reff sub?
 
You have misunderstood the example and what I was explaining. What you're asking for is exactly what is happening; a new instance is assigned the callers reference variable.
 
nismo,
You're not overwritting the source of the passed instance (to use your words, if I may) You want to point your original instance to the address of the new instance in memory. Check out my example:

VB.NET:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim firstInstance As Integer = 7
        Call refSub(firstInstance)
        MsgBox("After ByRef call:  " & firstInstance.ToString)
        firstInstance = 7
        Call valSub(firstInstance)
        MsgBox("After ByVal call: " & firstInstance.ToString)

    End Sub

    Private Sub refSub(ByRef ri1 As Integer)
        Dim ri2 As Integer = ri1
        MsgBox("New Instance: " & ri2.ToString)
        ri2 = 5
        MsgBox("Old Instance: " & ri1.ToString)
        ri1 = ri2
        MsgBox("Old Instance: " & ri1.ToString)
    End Sub

    Private Sub valSub(ByVal vi1 As Integer)
        Dim vi2 As Integer = vi1
        MsgBox("New Instance: " & vi2.ToString)
        vi2 = 5
        MsgBox("Old Instance: " & vi1.ToString)
        vi1 = vi2
        MsgBox("Old Instance: " & vi1.ToString)
    End Sub

In the refSub subroutine, a reference to the first instance of the object is passed in. I create a new instance of the same object (like you were doing) and then the code (ri1 = ri2) points the first instance to the second instance's address in memory. From the calling subroutine, you can see that the address reference of the original instance has changed. By contrast, the call to the valSub does not change the address reference of the original instance. I hope this helps. The = operator should work in the same fashion with your object reference as with the Integer object.
 
Last edited:
Back
Top