user1972656
user1972656

Reputation: 21

How can I access the property of a class in C# from Visual Basic?

I need help in the above topic. I have done everything required for my VB6 application to work with a C# dll. I got to a point where I am unable to access the property of a class to obtain the ID.

I am calling this C# function:

public resultRetrieveWIP RetrieveWIP(string serialNumber)
{
     string header = string.Format("Bearer {0}", User.Token);

     var request = new RestRequest();
     request.Method = Method.GET;
     request.Resource = "api/wips";
     request.AddParameter("serialNumber", serialNumber);
     request.AddHeader("Authorization", header);

     var client = new RestClient(_iFactoryURL);
     var response = client.Execute(request);
     var uri = client.BuildUri(request);
     _lastRequest = uri.ToString();
     _lastResponse = response.Content;

     try
     {
          var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore };
          return JsonConvert.DeserializeObject<resultRetrieveWIP>(response.Content,settings);
     }
     catch
     {
          throw new Exception(jsonErrorHandler("RetrieveWIP",response.Content));
     }
}

resultRetrieveWIP has type as follows:

    public class resultRetrieveWIP
    {
        public List<wip> wips { get; set; }
    }

    public class wip
    {
        public long id { get; set; }
    }

In my Visual Basic, I added the above CIMiFactory.dll as my reference then I do this:

Public oIFactory As New CIMiFactory.iFactory

Public Function iFactory_GetSubAssySN(ByVal strSerialNumber As String, ByVal strPartNumber As String) As String
    On Error GoTo errHandler

    Dim getWIP As New CIMiFactory.resultRetrieveWIP
    Dim wip As New CIMiFactory.wip
    iFactory_GetSubAssySN = ""

    Set getWIP = oIFactory.RetrieveWIP(strSerialNumber)

    **Set wip = getWIP.wips(0)**
    iFactory_GetSubAssySN = CStr(wip.id)

    Exit Function

errHandler:
    iFactory_GetSubAssySN = Err.Description
End Function

I have problem at the line code marked ** **.

It gave error.

"Wrong number of arguments or invalid property assignment".

What is the correct method to get the "id" property value?.

Upvotes: 2

Views: 303

Answers (2)

StayOnTarget
StayOnTarget

Reputation: 13048

As Étienne has said, generics cannot be exported to COM.

One option is to add a separate IEnumerable "wrapper" property just to be accessible through COM, like so:

public class resultRetrieveWIP
{
    public List<wip> wips { get; set; }

    [ComVisible(true)]
    public IEnumerable GetWipsCOM() => wips;
}

This way code internal to C# can use the correct fully-typed generic list without any changes, and VB6 or whatever other COM consumer can also access the data.

IEnumerable is supported by COM & VB6. ie you would be able to For Each through the result of GetWipsCOM() in VB6.

Upvotes: 2

&#201;tienne Laneville
&#201;tienne Laneville

Reputation: 5031

Generics are not supported in VB6, so you can't access List(Of T) items, which is what getWIP.wips(0) is doing. You can use a non-generic Collection instead, in your resultRetrieveWIP class:

public class resultRetrieveWIP
{
    public Collection wips { get; set; }
}

See the following article for more details: How To Use a .NET Class with Lists in VB6

Upvotes: 2

Related Questions