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]
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]