Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

  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?

  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);
 }

Posted

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".

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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.

Posted
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...)

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...