473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

VB's CreateObject for C#

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]

Oct 13 '08 #1
7 3031
On Oct 13, 12:12 pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
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
Oct 13 '08 #2
Thanks for the quick reply, Mr. Machin.

So what is this website trying to tell me?
(Short Link) http://tinyurl.com/4l2fp2
(Full Link)
http://tangiblesoftwaresolutions.com...eateObject.htm

When I was trying to find something similar to CreateObject(), this is what
I found.

Is that site just there to confuse people?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
On Oct 13, 12:12 pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
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
Oct 13 '08 #3
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?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
On Oct 13, 12:12 pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
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
Oct 13 '08 #4
That page was obsolete. Thanks for bringing it to our attention. It's now
removed.
--
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
"jp2msft" wrote:
Thanks for the quick reply, Mr. Machin.

So what is this website trying to tell me?
(Short Link) http://tinyurl.com/4l2fp2
(Full Link)
http://tangiblesoftwaresolutions.com...eateObject.htm

When I was trying to find something similar to CreateObject(), this is what
I found.

Is that site just there to confuse people?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
On Oct 13, 12:12 pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
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
Oct 14 '08 #5
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

"David Anton" wrote:
That page was obsolete. Thanks for bringing it to our attention. It's now
removed.
--
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
"jp2msft" wrote:
Thanks for the quick reply, Mr. Machin.

So what is this website trying to tell me?
(Short Link) http://tinyurl.com/4l2fp2
(Full Link)
http://tangiblesoftwaresolutions.com...eateObject.htm

When I was trying to find something similar to CreateObject(), this is what
I found.

Is that site just there to confuse people?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
On Oct 13, 12:12 pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
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
>
Oct 14 '08 #6
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
"jp2msft" wrote:
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

"David Anton" wrote:
That page was obsolete. Thanks for bringing it to our attention. It's now
removed.
--
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
"jp2msft" wrote:
Thanks for the quick reply, Mr. Machin.
>
So what is this website trying to tell me?
(Short Link) http://tinyurl.com/4l2fp2
(Full Link)
http://tangiblesoftwaresolutions.com...eateObject.htm
>
When I was trying to find something similar to CreateObject(), this is what
I found.
>
Is that site just there to confuse people?
>
"Ignacio Machin ( .NET/ C# MVP )" wrote:
>
On Oct 13, 12:12 pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
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
Oct 14 '08 #7
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...
On 10/13/2008 9:12 AM, jp2msft wrote:
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]

--
Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net
icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

.... FRA #284: Deep down everyone's a Ferengi.
Oct 16 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Daryl Davis | last post by:
Can I still write dll files in Visual basic.net?
1
by: dave | last post by:
I'm classic ASP developer and trying to switch to .net ...I'm newbie to ..net... In classic ASP for ADO connectivity i used to put below code in one separate file and used to include that file...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
5
by: Stuart Dee | last post by:
Hi, I have created a com+ component with vb dot net using jit activation and transactions If i call it like this x=createobject("mycomp") x.dispose
2
by: Ron | last post by:
Hello, I have to invoke instances of MS Excel and MS Access from my vb.net applications so that I can invoke procedures that reside in the Excel and Access applications from the vb.net...
3
by: wizzbangca | last post by:
Hi everyone. Having problems with a utility I am writing for work. The previous IT Director thoughtfully allowed 3 (2000, xp, 2003) versions of outlook to be installed rather than 1. Now I need...
0
by: quintesv | last post by:
hi all, Running VS.2003 , framework 1.1. I have a COM object written in Delphi which im trying to call through ASP.NET. The COM object connects to a folder on another machine and opens a...
14
by: shamirza | last post by:
Question Do ActiveX DLLs made in VB still need the VB runtimes on the machine? ________________________________________ Answer In a word, Yes. Visual Basic does not support what is known...
1
BezerkRogue
by: BezerkRogue | last post by:
I have created a VB Script to synchronize software versions and then launch an application on the system it is run against. The script runs and generates no errors but will not launch the second...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.