Convert VB.NEt to C# problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Howard

    Convert VB.NEt to C# problem

    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("V BAPP.Session")
    oAPP.Method1(us er, pass, db)
    oApp2 = oAPP.SessionIte m("APP2")
    var1 = oApp2.Propertie s.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.SessionCl ass();
    oAPP.Method1(re f User, ref Pass, ref DataSource);
    oApp2 = oAPP.get_Sessio nItem("APP2") as APP2.Class;
    var1 = oApp2.Propertie s.Item(........ ....
    }

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

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

    What am I missing?

    Thanks

  • Cor Ligthert [MVP]

    #2
    Re: Convert VB.NEt to C# problem

    Mike,

    Are you sure it is working in VB.Net

    This would normally not work in VB.Net

    oAPP = CreateObject("V BAPP.Session")
    oAPP.Method1(us er, pass, db)

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

    Cor


    Comment

    • Mike Howard

      #3
      Re: Convert VB.NEt to C# problem


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

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Convert VB.NEt to C# problem

        Mike,

        Then put in that VB.Net program in top Option Strict On

        Cor

        "Mike Howard" <mhoward736@gma il.comschreef in bericht
        news:1160022534 .413780.91630@m 73g2000cwd.goog legroups.com...
        >
        Yes, I'm sure it works in VB.Net - the code is in use right now and
        returns a the value I expect.
        >

        Comment

        • Mike Howard

          #5
          Re: Convert VB.NEt to C# problem


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

          If I do:

          App2.PropertyCl ass var1 = oApp2.Propertie s.Item(ref d) as
          App2.PropertyCl ass;

          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.WriteLi ne(var1.Value);

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

          Comment

          • Mike Howard

            #6
            Re: Convert VB.NEt to C# problem

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

            If I do:

            App2.PropertyCl ass var1 = oApp2.Propertie s.Item(ref d) as
            App2.PropertyCl ass;
            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.

            Comment

            • sloan

              #7
              Re: Convert VB.NEt to C# problem


              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.



              "Mike Howard" <mhoward736@gma il.comwrote in message
              news:1160027751 .866830.130220@ i42g2000cwa.goo glegroups.com.. .
              I have a solution but don't yet fully understand what's happening
              >
              If I do:
              >
              App2.PropertyCl ass var1 = oApp2.Propertie s.Item(ref d) as
              App2.PropertyCl ass;
              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.
              >

              Comment

              • David Anton

                #8
                RE: Convert VB.NEt to C# problem

                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.Get TypeFromProgID( "VBAPP.Session" );
                oAPP = System.Activato r.CreateInstanc e(oAPPType);
                oAPPType.Invoke Member("Method1 ",
                System.Reflecti on.BindingFlags .InvokeMethod, null, oAPP, new object[] {user,
                pass, db});
                oApp2 = oAPPType.Invoke Member("Session Item",
                System.Reflecti on.BindingFlags .InvokeMethod, null, oAPP, new object[]
                {"APP2"});
                var1 = oApp2.Propertie s["Date"].Value;
                }

                --
                David Anton
                Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                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


                "Mike Howard" wrote:
                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("V BAPP.Session")
                oAPP.Method1(us er, pass, db)
                oApp2 = oAPP.SessionIte m("APP2")
                var1 = oApp2.Propertie s.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.SessionCl ass();
                oAPP.Method1(re f User, ref Pass, ref DataSource);
                oApp2 = oAPP.get_Sessio nItem("APP2") as APP2.Class;
                var1 = oApp2.Propertie s.Item(........ ....
                }
                >
                I'm trying to figure out how to get the same value for var1 as
                oApp2.Propertie s.Item(.. wants a ref Object vIndex
                >
                When I add Object d = "Date"; and do oAppProperties. Item(ref d) it
                returns System.___ComOb ject
                >
                What am I missing?
                >
                Thanks
                >
                >

                Comment

                Working...