See my code. It is working, it writes to debug InstanceName of a Class2 instance (the name is Test).
I want to change my code so that in Class Form1 instead of line Test = New Class2(NameOf(Test)) there will be line Test = New Class2().
And assigning the InstanceName will be made automatically in the Sub New in the Class2. So that the Sub New would look something like this:
Sub New()
Me.InstanceName = **???Me.GetParent.Name???**
End Sub
Now this is the working example:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Test As Class2
Test = New Class2(NameOf(Test))
Debug.WriteLine($"InstanceName: {Test.InstanceName}")
End Sub
End Class
Public Class Class2
Public Property InstanceName
Sub New(InstanceName_ As String)
Me.InstanceName = InstanceName_
End Sub
End Class
Testis not the name of the instance, it's the name of a local variable.Sub Hello(world As Class2)for example - that instance would now be namedworld.