Link to home
Start Free TrialLog in
Avatar of tmccrank
tmccrank

asked on

Option Strict On disallows late binding

Hi,

I'm getting the above error on the following line of code:

Dim strCurrentQuestion As String = e.Item.DataItem("QuestionID").ToString

How can I get around the error? (other than going to OptStr Off of course)

Thanks
Avatar of davehunt00
davehunt00

You can turn off Strict On rules. You lose some notification, but might be best for what you're doing.

http://msdn2.microsoft.com/en-us/library/efwbatax.aspx 

Dave
Dim strCurrentQuestion As String = e.Item.DataItem("QuestionID").ToString
should be
Dim strCurrentQuestion As String = Ctype(e.Item.DataItem("QuestionID"),String)

HTH



Avatar of tmccrank

ASKER

Dave,  thanks, but I would prefer to keep Strict On rules.

Sammy, thanks, but I'm still getting the same error.

Jens
Lots of better experts than I, but I think you can't avoid the error with this compiler setting wtih the "e.Item.DataItem("QuestionID").ToString".  Because this is a runtime binding (vs one the compiler controls) the warning is basically telling you that it can't be sure you'll stuff the right data in and that it will implicitly convert. It's not saying it can't work, just that it might not.
try it in the databound sub
 Protected Sub MyGird_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
Dim strCurrentQuestion As String = CType(e.Item.DataItem("QuestionID"),String)
    End Sub
or you can try
Dim strCurrentQuestion As String = DataBinder.Eval(e.Item.DataItem, "QuestionID")


Good luck
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Bob,

I'm new at  .NET, could you tell me how to use e.Item.DataItem.GetType().Name to get the type that should replace DataRowView?

I just did this:  Dim strType As String = e.Item.DataItem.GetType().Name

And tried to use the variable strType:
Dim strCurrentQuestion As String = CType(CType(e.Item, RepeaterItem).DataItem, strType("QuestionID").ToString()

I'm getting design-time error with QuestionID: Array bounds cannot appear in type specifiers.

I'm likely way off in how I'm using the strType variable, your help is appreciated.
Jens
That was only a temporary test to see what the type is, not to be used in the end.

Try this:
  Dim strCurrentQuestion As String = CType(CType(e.Item, RepeaterItem).DataItem)("QuestionID").ToString()

You can quickly see how cumbersome Option Strict can be.

Bob