|
-
Jan 27th, 2012, 10:33 AM
#1
Thread Starter
PowerPoster
[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.
-
Jan 27th, 2012, 10:40 AM
#2
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.
-
Jan 27th, 2012, 10:46 AM
#3
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)
-
Jan 27th, 2012, 12:58 PM
#4
Thread Starter
PowerPoster
Re: Option Strict On Disallows Late Binding
 Originally Posted by MattP
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.
 Originally Posted by MattP
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.
-
Jan 27th, 2012, 01:14 PM
#5
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.
-
Jan 27th, 2012, 01:26 PM
#6
Thread Starter
PowerPoster
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.
-
Jan 27th, 2012, 01:43 PM
#7
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)
-
Jan 27th, 2012, 02:44 PM
#8
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.
-
Jan 27th, 2012, 03:48 PM
#9
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|