VB's CreateObject for C#

J

jp2msft

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:
[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
[/vb code]

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?

[cs 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);
}
[/cs code]
 
I

Ignacio Machin ( .NET/ C# MVP )

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.

IIRC CreateObject is used to create a COM object. If that is the case
all you have to do is add the reference to the dll in your project.
after that you just create an instance of it as you usually do with a
class .net
 
J

jp2msft

Oh wait: I still can not access the individual objects in the initial object.

Whenever I try to index them, the error is that they do not contain a
'GetEnumerator' and that there is no definition for the 'Name' or 'Value'
parameters in the objects.

How would I map these?
 
J

jp2msft

Mr. Anton,

Thanks for that reply. I spent several hours trying to figure out how to get
that version to work, and I could not understand why it was so hard to grasp.

Regards,
~Joe
 
D

David Anton

Activator.CreateInstance is similar in the .NET world to what 'CreateObject'
is for COM objects - i.e., it lets you get a late-bound instance of a type,
but the similarity is inappropriate for conversion from VB to C# since one
deals with instances of COM types and the other deals with instances of .NET
types.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
M

Michael J. Ryan

Probably already answered, but CreateObject simply instantiates a given COM
object... you should be able to do something like...

Lblvw.Document objDoc = new Lblvw.Document();
....

note, if this is a COM object, you will have to create a reference in your
project before you can use it.. this should create a stub/wrapper for
accessing your COM object in this way...


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:
[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
[/vb code]

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?

[cs 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);
}
[/cs code]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top