Passing an object from VB6 to C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ymak
    New Member
    • Nov 2016
    • 5

    Passing an object from VB6 to C#

    Hello,

    I am trying to use C# code from VB6 using COM wrapper.
    Is there an example on how to pass an object from VB6 to C#?
    For example my C# method is public void PassObject(Item itm) where Item is the C# class.
    Trying to pass a similar class from VB6 I get the error "runtime error 438 object doesn't support this property or method"

    Thank you
  • ymak
    New Member
    • Nov 2016
    • 5

    #2
    My fault.
    The "Call" keyword in front of the Sub name was missing (I have not used VB till now)
    However now I get "runtime error 430 class does not support automation or does not support expected interface"

    Comment

    • Zakoss
      New Member
      • Nov 2016
      • 6

      #3
      You could use something like this in c#:
      Code:
      [Guid("fb5e929a-2f8b-481e-9516-97edf5099df4")]
      [ComVisible(true)]
      public interface myInterface{
      public void addObject(string key, string value);
      }
      And in your class, you could have this:
      Code:
      private collection
      public addObject(string key, string value)
      {
      collection.Add(key, value);
      }
      This should allow you to call addObject in vb6 and passing your data. Then .net will add it to a collection, so instead of passing your whole collection from vb6 to .net, you pass them one by one

      Comment

      • ymak
        New Member
        • Nov 2016
        • 5

        #4
        Thank you Zakoss.
        I have already implemented a solution like that.
        I am just wondering if it possible to use a class instead of separate parameters.
        Following that I was planing to pass an array of objects.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I have never used .NET objects within a VB6 code base; however you should be able to write interop code to expose the classes and methods you want to consume if you follow the guidelines for Interoperabilit y.

          I would research the topic using the MSDN library

          A very general starting point would be:

          According to the second article you should be able to pass classes if the classes are implemented properly for that process.

          Comment

          • ymak
            New Member
            • Nov 2016
            • 5

            #6
            Thank you Frinavale
            After playing around I have managed to pass an object as parameter to c# method.
            I still haven't managed to pass an array of objects.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              ymak,

              I'm sorry but in all of my interop usage I have never been able to pass arrays of classes.

              I didn't experience any errors but the data did not translate properly between the two memory heaps and so it ended up getting messed up (from what I remember...it has been a long time).

              I was hoping you would have better luck.

              If you are successful, post back and let me know what you did.

              Comment

              • ymak
                New Member
                • Nov 2016
                • 5

                #8
                I'll probably implement a solution like Zakoss suggested.
                Passing one by one and storing them in a collection

                Comment

                Working...