Results 1 to 6 of 6

Thread: VB.NET Late Binding in C#

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    VB.NET Late Binding in C#

    I had a VB6 code long time ago then I convert to VB.NET. Now I am working on a version in C#, Can I totally get rid of NewLateBinding because C# doesn't have such thing? I means not making two references of using Microsoft.VisualBasic and using Microsoft.VisualBasic.CompilerServices.

    Code:
    object objectValue = WebBrowser1.Document.InvokeScript("z");
    while (!IsDBNull(NewLateBinding.LateGet(objectValue, null, "range", new object[0], null, null, null)))
    {
       //...
    }
    Last edited by DaveDavis; Nov 30th, 2020 at 08:26 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB.NET Late Binding in C#

    You should probably have fixed the VB.NET code to get rid of the late-binding first. What type is the actual object you are dealing with? Is there a reason that you can't cast as that type?

    If you really must use late-binding in C# then you can use the dynamic keyword these days. It works slightly differently than in VB but achieves the same result.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB.NET Late Binding in C#

    By the way, as you have asked it, this is a C# question. I won't do anything yet as it may turn out that you should be making changes in VB first anyway. If not, I will ask the mods to move this thread to the C# forum.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: VB.NET Late Binding in C#

    Quote Originally Posted by jmcilhinney View Post
    What type is the actual object you are dealing with?
    it is WebBrowser1.Document.

    Code:
    object objectValue = WebBrowser1.Document.InvokeScript("z");
    while (!IsDBNull(NewLateBinding.LateGet(objectValue, null, "range", new object[0], null, null, null)))
    {
       //...
    }

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB.NET Late Binding in C#

    Quote Originally Posted by DaveDavis View Post
    it is WebBrowser1.Document.
    That's not an answer to the question I asked. The issue here is that you are declaring objectValue as type Object and so you only have compile-time access to members of that type. The object that you are assigning to that variable is obviously a more specific type and you want to access a member - seemingly a range field or property - of that type. In order to access that member without using late-binding, you need to cast a reference to that object as that type. That tells the compiler that it can access members of that type. That is the type I am asking about. You should know what that type is. Once you do know, you can do something like this in VB:
    vb.net Code:
    1. Dim objectValue = DirectCast(WebBrowser1.Document.InvokeScript("z"), SomeType)
    The variable is then the correct type so you can use early-binding to access members of that type. The equivalent in C# would be this:
    csharp Code:
    1. var objectValue = (SomeType) WebBrowser1.Document.InvokeScript("z");
    Is there a reason that you cannot access that type and cast as it? There may be, in which case this is a C# question and can be moved to the C# forum to be addressed in a manner specific to C#. If there isn't, it's a VB question because you should be using early-binding in the VB code first and then the conversion to C# is a doddle.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: VB.NET Late Binding in C#

    Quote Originally Posted by jmcilhinney View Post
    That's not an answer to the question I asked. The issue here is that you are declaring objectValue as type Object and so you only have compile-time access to members of that type. The object that you are assigning to that variable is obviously a more specific type and you want to access a member - seemingly a range field or property - of that type. In order to access that member without using late-binding, you need to cast a reference to that object as that type. That tells the compiler that it can access members of that type. That is the type I am asking about. You should know what that type is. Once you do know, you can do something like this in VB:
    vb.net Code:
    1. Dim objectValue = DirectCast(WebBrowser1.Document.InvokeScript("z"), SomeType)
    The variable is then the correct type so you can use early-binding to access members of that type. The equivalent in C# would be this:
    csharp Code:
    1. var objectValue = (SomeType) WebBrowser1.Document.InvokeScript("z");
    Is there a reason that you cannot access that type and cast as it? There may be, in which case this is a C# question and can be moved to the C# forum to be addressed in a manner specific to C#. If there isn't, it's a VB question because you should be using early-binding in the VB code first and then the conversion to C# is a doddle.
    My VB.NET code doesn't have cast because we used Late binding:
    Code:
    Dim objectValue As Object =  AddressOf WebBrowser1.Document.InvokeScript("z")
    While Not Information.IsDBNull(NewLateBinding.LateGet(objectValue, Nothing, "range", New Object(- 1) {}, Nothing, Nothing, Nothing))
     
    Dim singleWord As String = Conversions.ToString(NewLateBinding.LateGet(objectValue, Nothing, "word", New Object(- 1) {}, Nothing, Nothing, Nothing))
    
    End While
    Edited: forgot to mention that InvokeScript("z") function is to execute a short javascript codes and return a custom object.
    Code:
    function z() 
    {
           var t = document.body.createTextRange();
           var a = t.duplicate();
           //...          
           return 
           {
              range : a,
              word: a.text,
              index: y
           };
    }
    so, for my case, how to retrieve objectValue's range/word/index without using NewLateBinding.LateGet?
    Last edited by DaveDavis; Dec 1st, 2020 at 08:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width