tags:

views:

64

answers:

2

*strong text*I have a base class with a method in it for synchronizing data. However I need to be able to create a new object in the process that is the same as the parent object that inherited the base class.

I want to do something like this:

Sub Transfer(ByVal OBType As BaseClass)
     Dim A as new typeof(OBType)
     A.doSomething()
End Sub

EDIT

I appologize for any confusion

My use case is that I’m transfering data between two objects and the method is responsible for merging the changes between them. When the method is called it gets a set of records and then must call a transfer method that synches them. When execution GetDataRow I need to have a new version of the object and I also need a new version to complete the transfer.

Here is a stripped down version of the code:

GetDataRow creates the object form the datarow and transfer data then compares it and makes the necessary changes.

  Sub Transfer(ByVal OBType As BaseBLLClass)
        'Get a list from each side and synch
        Dim DT As DataTable = GetDataMethod() ‘Gets data to transfer
        Dim A As OBType
        Dim B As OBType
        For Each Row As DataRow In DT.Rows
             A = New OBType
             B = New OBType
             GetDataRow(A, Row).TransferData(B)
        Next
  End Sub

Solution

  Public Shared Sub Base_Transfer(Of OBType As BaseBLLClass)()
        'Get a list from each side and synch
        Dim DT As DataTable = GetDataMethod() ‘Gets data to transfer
        Dim A As BaseClassUIM = Activator.CreateInstance(GetType(OBType))
        Dim B As BaseClassUIM = Activator.CreateInstance(GetType(OBType))
        For Each Row As DataRow In DT.Rows
             A = Activator.CreateInstance(GetType(OBType))
             B = Activator.CreateInstance(GetType(OBType))
             GetDataRow(A, Row).TransferData(B)
        Next
  End Sub
+1  A: 

You can use Activator.CreateInstance to construct the new object with the appropriate type. Once you've done that, you can cast it to your base class.

Reed Copsey
Is that what he needs? Seems kind of... useless in the way he's asking to do it.
Will
In v1 of this question, this directly answered what he was asking. There was no real explanation of what was being attempted, just how to do this. However, this is sometimes helpful - but usually requires using reflection to copy other values, as well, to be truly useful...
Reed Copsey
Good observation Reed. I do use reflection when synching the data based on the properties of the objects but that is encapsulated in the method that does the synch which still needs to be passed valid objects to compare. Your answer was what I needed.
Middletone
+1  A: 

I'm not aware of how it would look in VB, but in C# you could do that with generics like this:

public void Transfer<T>(T item) where T: BaseClass, new()
{
    var a = new T();
    a.DoSomething();
}

Here's my feeble attempt at writing the same in VB:

Sub Transfer(Of T As {BaseClass, New})(ByVal OBType As T)
     Dim A as new T
     A.doSomething()
End Sub
Mark Seemann
This probably doesn't do what you think it does. T is resolved at compile time, not run time.
recursive
Yes, I'm aware of that. It's not clear to me whether that's what being asked or not, though...
Mark Seemann
Good answer. I had to combine your code with Reeds to get the solution but it works. I'll post the code.
Middletone