Results 1 to 9 of 9

Thread: [RESOLVED] Option Strict On Disallows Late Binding

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Resolved [RESOLVED] Option Strict On Disallows Late Binding

    I am working with another programmer's code. Had it been my code from scratch, option strict on would've been set. But it wasn't. So I turned it on and I have 46 errors. That doesn't bother me as they're usually simple issues but I do not understand late binding. What is wrong with this code?

    Code:
     Dim Level2ApproverList As New ListItemCollection
                For Each approver As String In Level2Approvers
                    Dim ItemArray As Array = Split(approver, ",")
                    Dim NewListItem As New ListItem
    
                    NewListItem.Value = ItemArray(0)               
                    NewListItem.Text = IIf(UBound(ItemArray) >= 1, ItemArray(1), String.Empty)
                    Level2ApproverList.Add(NewListItem)
                Next
    I am getting the late binding error on the blue lines.

    Want to know what really ticks me off? When I googled this and went to Microsoft it said: To correct this error
    Change Option Strict to Off, by removing the On after it or explicitly specifying Off.


    Seriously? Maybe I'm too anal, but they should be telling you how to fix it, not telling you how to run away from it. Can I find that out here? Thanks.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Option Strict On Disallows Late Binding

    Try changing the type to String() and use .NET's String.Split.

    Code:
    Dim ItemArray() As String = approver.Split(","c)
    As it is your ListItemCollection will always be empty when you hit the For loop since you're newing it up on the previous line. I'm assuming there's code to fill it that you've cut out. If you haven't you'll get an error here ItemArray(0) since the array will be empty.

  3. #3
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Option Strict On Disallows Late Binding

    I'm not totally sure, but I think it is because it is complaining about you using the array class, which you shouldn't use directly
    Code:
    Dim ItemArray() As String = approver.Split(","c)
    Or you can use the overload to remove empty entries.
    Code:
    Dim ItemArray() As String = approver.Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Option Strict On Disallows Late Binding

    Quote Originally Posted by MattP View Post
    Try changing the type to String() and use .NET's String.Split.

    Code:
    Dim ItemArray() As String = approver.Split(","c)
    Thanks. I was looking at the instruction, not where the array was defined.
    Quote Originally Posted by MattP View Post
    As it is your ListItemCollection will always be empty when you hit the For loop since you're newing it up on the previous line. I'm assuming there's code to fill it that you've cut out. If you haven't you'll get an error here ItemArray(0) since the array will be empty.
    Do you have Level2Approvers mixed up with Level2ApproversList? Here is a little bit more of the code:
    Code:
                Dim Level2Approvers As Array = Split(ev.ApproverID2, "|")
    
                Dim Level2ApproverList As New ListItemCollection
                For Each approver As String In Level2Approvers
                    ' Dim ItemArray As Array = Split(approver, ",")
                    Dim ItemArray() As String = approver.Split(","c)
                    Dim NewListItem As New ListItem
    
                    NewListItem.Value = ItemArray(0)
                    NewListItem.Text = CStr(IIf(UBound(ItemArray) >= 1, ItemArray(1), String.Empty))
                    Level2ApproverList.Add(NewListItem)
                Next
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Option Strict On Disallows Late Binding

    You're right I did have Level2Approvers and Level2ApproversList mixed up.

    I would like to point out that this line

    Code:
    Dim Level2Approvers As Array = Split(ev.ApproverID2, "|")
    Would be better stated as

    Code:
    Dim Level2Approvers() As String = ex.ApproverID2.Split("|"c)
    Same reason as the ItemArray As Array issue from before.

    While you're going through and correcting the code you may want to change IIf to If and UBound to ItemArray.Length. Not completely necessary but I like to get the VB6 carry-overs out of code when I see them.

  6. #6

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Option Strict On Disallows Late Binding

    I agree with you on the first point, at least for consistency's sake. I just ignored them because I was happy when I finally got my clean compile.

    What is wrong with IIf? And UBound? They are really VB6 carry-overs? I thought IIf was just a concise way of writing a simple if-statement. Sometimes I use it, sometimes I don't.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  7. #7
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Option Strict On Disallows Late Binding

    You can just replace IIf with If, they basically do the same thing but If is the .net version. And as for Ubound, the direct replacement is ArrayName.GetUpperBound(Dimension)

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  8. #8
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Option Strict On Disallows Late Binding

    UBound is an old VB6 carry over function which expects an array parameter. Array.GetUpperBound will return the index of the last item in the array and Array.Length will give the actual number of elements in the array.

    IIf will return an Object hence the need for your cast to String once it's evaluated while If will return the type that you specify in the true and false return statements.

    IIf is also not short-circuited so both expressions will always be evaluated.

  9. #9

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: Option Strict On Disallows Late Binding

    Thanks for your help and the info.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

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