Results 1 to 3 of 3

Thread: [RESOLVED] Late-bound error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Resolved [RESOLVED] Late-bound error

    I have a few different classes which represents stuff like inventory screen, character screen etc.

    They all have different properties, but what they all have in common is that they have a property called "Rect", which represents where that window gets drawn in relation to the screen.
    I have created an object variable called "DragWindow", which is supposed to reference either of these, and change its "Rect" property (amongst others) at my leisure.

    After some testing I can change most of the properties via this object variable, but not rect for whatever reason. I get this error:

    Late-bound assignement to a field of a value type 'Rectangle' is not valid when 'Rectangle' is the result of a late-bound expression.

    Code:
    DragWindow = WindowLoot
    
    DragWindow.titletext = "TEST" (working)
    DragWindow.rect.x += 1 (throws error)
    Last edited by Nirwanda; Dec 1st, 2016 at 05:22 PM.

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

    Re: Late-bound error

    It's an issue because of the fact that it's a value type and has nothing really to do with late binding. Unlike if it was a reference type, when you get 'DragWindow.rect' you are getting a copy of the Rectangle stored in that property rather that a reference to the object itself. That means that 'DragWindow.rect.x += 1' would be incrementing the 'x' property of a copy of the original Rectangle rather than the original Rectangle, thus it would not be doing anything useful. Whether you were using early binding or late binding, what you actually need to do is this:
    vb.net Code:
    1. Dim rect As Rectangle = DragWindow.Rect
    2.  
    3. rect.X += 1
    4. DragWindow.Rect = rect
    In that case, you're still getting a copy but, instead of immediately discarding the modified copy, you're writing it back over the original.

    By the way, you really shouldn't be using late binding if you can possibly avoid it. If you have two types that both have a 'Rect As Rectangle' property then define an interface with that property and then have both those type implement it. You can then access the Rect property using early binding by casting both types as that interface. That way, you can turn Option Strict On as you should and avoid any late binding or implicit conversion issues.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Late-bound error

    That works, thanks a lot!

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