Results 1 to 5 of 5

Thread: help translating vb code to c#

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    help translating vb code to c#

    i have this vb.net code, which may not even work in vb.net itself, but it's an example someone posted. It is suppose to copy all the properties of an object without having to code each individual property.

    Code:
    For Each [property] As PropertyInfo In o2.GetType().GetProperties()
                [property].SetValue(o1, [property].GetValue(o2, Nothing), Nothing)
            Next [property]

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: help translating vb code to c#

    You can use the VB.NET > C# converter page found here -

    http://www.developerfusion.co.uk/uti...btocsharp.aspx
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: help translating vb code to c#

    If you want to copy an object, just use binary serialization.

    You don't need to think about propeties at all.
    I don't live here any more.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: help translating vb code to c#

    Quote Originally Posted by benmartin101
    i have this vb.net code, which may not even work in vb.net itself, but it's an example someone posted. It is suppose to copy all the properties of an object without having to code each individual property.

    Code:
    For Each [property] As PropertyInfo In o2.GetType().GetProperties()
                [property].SetValue(o1, [property].GetValue(o2, Nothing), Nothing)
            Next [property]
    This shouldn't be difficult to convert at all. It's just syntax.

    Also keep in mind that you don't need the icky brackets around "property" to differentiate it from "Property" in C#, since C# is case sensitive.

    Code:
    foreach(PropertyInfo property in o2.GetType().GetProperties())
    {
       property.SetValue(o1, property.GetValue(o2, null), null);
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: help translating vb code to c#

    thanks for the responses.

    I'll try your suggestion sunburnt.

    Wossname, I tried using serialization, but you see, I'm using a COM object. The class that i'm using is in a COM object and when I try to binary serialize, it won't let me. I think it's because I have to declare the class as being <serializable>. Unfortunately, I don't know how to make classes from a COM object to be serializable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width