Binding And Invoking

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I've got some old VB code that I am trying to convert to C#.

The old VB code uses CreateObject, which is not supported in C#.

I've been doing some reading on how to get information, but I just can't seem to get everything I need to get the C# application to compile.

Maybe someone here can see what I'm missing.

Here is the old VB code:
Code:
  Sub CreateLabel()
    Dim objDoc As Object = CreateObject("Lblvw.Document")
    objDoc.Open("C:\Temp\LabelView.dat", True)
    Dim LastError As String = objDoc.LastError
    For Each FLD As Object In objDoc.LabelFields
      Dim str1 As String
      Select Case (FLD.Name)
        Case "CustPartNum"
          FLD.Value = FLD.Name
        Case "Qty"
          FLD.Value = FLD.Name
        Case "Date"
          FLD.value = FLD.Name
        Case "Customer"
          FLD.value = "Customer"
      End Select
      Console.WriteLine("{0}: {1}", FLD.Name, FLD.Value)
    Next
    objDoc.Close()
  End Sub
Below is as far as I can seem to get using C#. It fails at the "foreach" loop because it can not do a foreach loop on an object. I could change that, but next it can't access the individual item's properties in the objects either.

Could someone offer me some guidance?
Code:
  public void CreateLabel() {
    System.Type objDocType = System.Type.GetTypeFromProgID("Lblvw.Document");
    object objDoc = System.Activator.CreateInstance(objDocType);
    objDocType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, objDoc, new object[] {"C:\\Temp\\LabelView.dat", true});
    string LastError = objDocType.InvokeMember("LastError", System.Reflection.BindingFlags.GetProperty, null, objDoc, null);
    foreach (object FLD in objDocType.InvokeMember("LabelFields", System.Reflection.BindingFlags.GetProperty, null, objDoc, null)) {
      string str1 = null;
      switch (FLD.Name) {
        case "CustPartNum":
          FLD.Value = FLD.Name;
        break;
        case "Qty":
          FLD.Value = FLD.Name;
          break;
        case "Date":
          FLD.Value = FLD.Name;
          break;
        case "Customer":
          FLD.Value = "Customer";
          break;
      }
      Console.WriteLine("{0}: {1}", FLD.Name, FLD.Value);
    }
    objDocType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, objDoc, null);
  }
 
Have you added a reference to the binary that contains Lblvw.Document ?

After you do that, you should be able to add the necessary using's at the top of your source file and treat it like an object.

You'll probably need to add the reference as a "COM Reference".
 
That part is fine. Notice the VB code calls each field as an object from the document then reads the field's name and sets the field's value.

In C# (or even if I updated them to VB.NET), I don't know how to parse the object fields or their object information in the fields. .NET does not like working on objects.

I've contacted the manufacturer, but they did not know. They are still only supporting VB6, and apparently that is enough to keep their product selling just fine.
 
After you've created the reference, you should be able to use the objects as they are defined in the library, you shouldn't need to use reflection. Instead of looping foreach(object...) you'll want to try something like foreach(Lblvw.Document.Field...)
 
Going from VB6 to .NET (first VB, then C#) was difficult for me, because of issues exactly like this one. It took me a while before I figured out that when you add a reference to a COM object, a type of wrapper class is created so you can access the COM object from .NET.
 
Back
Top