DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Micki Guest

    Interface question



    I have several classes implementing the same interface, let's call it "IMyData".

    In the following method I want to copy the IMyData data from an object to
    a new object of different type but implementing the same common interface
    (IMyData).

    The rest of the question is inline in the following function example:

    Public Sub AddObjectToControl(ByVal Data As IMyData)
    Dim MLI As New MyListItem()

    Now I wan't to assign all the properties in IMyData
    to CType(MLI,IMyData). Is there some better (shorter) way to do
    this than assigning each property separately like this?
    MLI.Property1 = Data.Property1
    MLI.Property2 = Data.Property2
    a.s.o.
    ??

    Me.ListItems.Add(FItem)
    End Sub

    Thanks,
    Micki

  2. #2
    Jesús Rodríguez Guest

    Re: Interface question

    If you really have lots of properties, you can use Reflection to acomplish
    this task, cycling through the interface properties:
    <NotTestedAndUglyAndProbablyIncorrect>
    for each p in MyType.GetProperties()
    p.SetProperty(dst,p.GetProperty(src,null),null)
    next
    </NotTestedAndUglyAndProbablyIncorrect>

    You could event use custom attributes to specify which properties to
    transfer.

    "Micki" <mjs@nospam.beamex.com> escribió en el mensaje
    news:3affc134$1@news.devx.com...
    >
    >
    > I have several classes implementing the same interface, let's call it

    "IMyData".
    >
    > In the following method I want to copy the IMyData data from an object to
    > a new object of different type but implementing the same common interface
    > (IMyData).
    >
    > The rest of the question is inline in the following function example:
    >
    > Public Sub AddObjectToControl(ByVal Data As IMyData)
    > Dim MLI As New MyListItem()
    >
    > Now I wan't to assign all the properties in IMyData
    > to CType(MLI,IMyData). Is there some better (shorter) way to do
    > this than assigning each property separately like this?
    > MLI.Property1 = Data.Property1
    > MLI.Property2 = Data.Property2
    > a.s.o.
    > ??
    >
    > Me.ListItems.Add(FItem)
    > End Sub
    >
    > Thanks,
    > Micki




  3. #3
    Micki Guest

    Re: Interface question


    "Jesús Rodríguez" <alsagajesus@arrakis.es> wrote:
    >If you really have lots of properties, you can use Reflection to acomplish
    >this task, cycling through the interface properties:


    Thank you!

    If there really are not any simpler way to do this, I can se at least one
    situation where C++ would have benefits over VB/C#.

    If the same example were done in C++ with multiple inheritance, I could just
    cast the object I want to copy the data to, to the base object containing
    the data and then do a memcpy aka "=".



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links