Convert VB.NEt to C# problem

M

Mike Howard

I'm trying to convert the following (simplified) VB.Net code to C#,
that makes use of some externally developed COM code written in VB6.

VB.Net Code

Sub Main()
Dim oAPP As Object
Dim oApp2 As APP2.Class

Dim user As String
Dim pass As String
Dim db As String
Dim var1 as String

User = "user"
pass = "password"
db = "test"

oAPP = CreateObject("VBAPP.Session")
oAPP.Method1(user, pass, db)
oApp2 = oAPP.SessionItem("APP2")
var1 = oApp2.Properties.Item("Date").Value
End Sub

The C# Code looks like
static void Main(string[] args)
{
VBAPP.Session oApp;
APP2.Class oApp2;
string User = "user";
string pass = "password";
string db = "test";
string var1;

oAPP = new VBAPP.SessionClass();
oAPP.Method1(ref User, ref Pass, ref DataSource);
oApp2 = oAPP.get_SessionItem("APP2") as APP2.Class;
var1 = oApp2.Properties.Item(............
}

I'm trying to figure out how to get the same value for var1 as
oApp2.Properties.Item(.. wants a ref Object vIndex

When I add Object d = "Date"; and do oAppProperties.Item(ref d) it
returns System.___ComObject

What am I missing?

Thanks
 
C

Cor Ligthert [MVP]

Mike,

Are you sure it is working in VB.Net

This would normally not work in VB.Net

oAPP = CreateObject("VBAPP.Session")
oAPP.Method1(user, pass, db)

(The code you make for this in C# is by the way complete different)

Cor
 
M

Mike Howard

Yes, I'm sure it works in VB.Net - the code is in use right now and
returns a the value I expect.
 
M

Mike Howard

Thanks, but I'm trying to get rid of the VB code

If I do:

App2.PropertyClass var1 = oApp2.Properties.Item(ref d) as
App2.PropertyClass;

and step through the code I can see in the watch window

var1.Value has a Value of "04-Oct-2006" and this gets returned when I
do a var1.Value in the immediate window, but I can't complie and
execute with Console.WriteLine(var1.Value);

I know I'm missing something basic here...
 
M

Mike Howard

I have a solution but don't yet fully understand what's happening

If I do:

App2.PropertyClass var1 = oApp2.Properties.Item(ref d) as
App2.PropertyClass;
string f = var1.get_Value();

f contains what I was expecting.

get_Value is not one of the Options listed by intellisense and all the
other options that are such as var1.Length, var1.access worked as I
expected.
 
S

sloan

Mike

As a general rule of thumb. If you start with VB.net code and translate it.
The at the top of the VB.net COde, put the following declarations.


Option Explicit On
Option Strict On

Yes , I know you're not keeping the vb.net code.
But using those 2 will make a translation easier............... because C#
has "built in" .. "Option Explicit On" and "Option Strict On" (for lack of a
better way to describe it)

After it compiles in VB.net (with those 2 declarations), then you can work
on the translation in an easier fashion.
 
G

Guest

Just for your information, if you want the equivalent C# late-binding approach:
public void Main()
{
object oAPP = null;
APP2.Class oApp2 = null;

string user = null;
string pass = null;
string db = null;
string var1 = null;

user = "user";
pass = "password";
db = "test";

System.Type oAPPType = System.Type.GetTypeFromProgID("VBAPP.Session");
oAPP = System.Activator.CreateInstance(oAPPType);
oAPPType.InvokeMember("Method1",
System.Reflection.BindingFlags.InvokeMethod, null, oAPP, new object[] {user,
pass, db});
oApp2 = oAPPType.InvokeMember("SessionItem",
System.Reflection.BindingFlags.InvokeMethod, null, oAPP, new object[]
{"APP2"});
var1 = oApp2.Properties["Date"].Value;
}

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 

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